1+ import { existsSync } from 'node:fs'
12import { cp } from 'node:fs/promises'
23import { extname , join } from 'node:path/posix'
34
5+ import { NEXT_RUNTIME_STATIC_ASSETS } from './constants.js'
46import type { FrameworksAPIConfig , OnBuildCompleteContext } from './types.js'
57
68export 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