Skip to content

Commit 5215cf8

Browse files
committed
static assets trailing slashes, public and some constants moving
1 parent a298b3b commit 5215cf8

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

src/adapter/adapter.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { dirname } from 'node:path'
33

44
import type { NextAdapter } from 'next-with-adapters'
55

6+
import { NETLIFY_FRAMEWORKS_API_CONFIG_PATH } from './constants.js'
67
import { onBuildComplete as onBuildCompleteForHeaders } from './header.js'
78
import {
89
modifyConfig as modifyConfigForImageCDN,
@@ -12,8 +13,6 @@ import { onBuildComplete as onBuildCompleteForMiddleware } from './middleware.js
1213
import { onBuildComplete as onBuildCompleteForStaticAssets } from './static-assets.js'
1314
import { FrameworksAPIConfig } from './types.js'
1415

15-
const NETLIFY_FRAMEWORKS_API_CONFIG_PATH = '.netlify/v1/config.json'
16-
1716
const adapter: NextAdapter = {
1817
name: 'Netlify',
1918
modifyConfig(config) {

src/adapter/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ export const PLUGIN_DIR = join(MODULE_DIR, '../..')
88
const packageJSON = JSON.parse(readFileSync(join(PLUGIN_DIR, 'package.json'), 'utf-8'))
99

1010
export const GENERATOR = `${packageJSON.name}@${packageJSON.version}`
11+
12+
export const NETLIFY_FRAMEWORKS_API_CONFIG_PATH = '.netlify/v1/config.json'
13+
export const NETLIFY_FRAMEWORKS_API_EDGE_FUNCTIONS = '.netlify/v1/edge-functions'
14+
export const NEXT_RUNTIME_STATIC_ASSETS = '.netlify/static'

src/adapter/middleware.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import { dirname, join, parse, relative } from 'node:path/posix'
44
import { glob } from 'fast-glob'
55
import { pathToRegexp } from 'path-to-regexp'
66

7-
import { GENERATOR, PLUGIN_DIR } from './constants.js'
7+
import { GENERATOR, NETLIFY_FRAMEWORKS_API_EDGE_FUNCTIONS, PLUGIN_DIR } from './constants.js'
88
import type { FrameworksAPIConfig, NextConfigComplete, OnBuildCompleteContext } from './types.js'
99

10-
const NETLIFY_FRAMEWORKS_API_EDGE_FUNCTIONS = '.netlify/v1/edge-functions'
1110
const MIDDLEWARE_FUNCTION_NAME = 'middleware'
1211

1312
const MIDDLEWARE_FUNCTION_DIR = join(

src/adapter/static-assets.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { existsSync } from 'node:fs'
12
import { cp } from 'node:fs/promises'
23
import { extname, join } from 'node:path/posix'
34

5+
import { NEXT_RUNTIME_STATIC_ASSETS } from './constants.js'
46
import type { FrameworksAPIConfig, OnBuildCompleteContext } from './types.js'
57

68
export async function onBuildComplete(
@@ -13,11 +15,18 @@ export async function onBuildComplete(
1315
try {
1416
let distPathname = staticFile.pathname
1517
if (extname(distPathname) === '' && extname(staticFile.filePath) === '.html') {
18+
// FEEDBACK: should this be applied in Next.js before passing to context to adapters?
19+
if (ctx.config.trailingSlash && !distPathname.endsWith('/')) {
20+
distPathname += '/'
21+
} else if (!ctx.config.trailingSlash && distPathname.endsWith('/')) {
22+
distPathname = distPathname.slice(0, -1)
23+
}
24+
1625
// if pathname is extension-less, but source file has an .html extension, preserve it
17-
distPathname += '.html'
26+
distPathname += distPathname.endsWith('/') ? 'index.html' : '.html'
1827
}
1928

20-
await cp(staticFile.filePath, join('./.netlify/static', distPathname), {
29+
await cp(staticFile.filePath, join(NEXT_RUNTIME_STATIC_ASSETS, distPathname), {
2130
recursive: true,
2231
})
2332
} catch (error) {
@@ -27,5 +36,13 @@ export async function onBuildComplete(
2736
}
2837
}
2938

39+
// FEEDBACK: files in public directory are not in `outputs.staticFiles`
40+
if (existsSync('public')) {
41+
// copy all files from public directory to static assets
42+
await cp('public', NEXT_RUNTIME_STATIC_ASSETS, {
43+
recursive: true,
44+
})
45+
}
46+
3047
return frameworksAPIConfig
3148
}

0 commit comments

Comments
 (0)