Skip to content

Commit 3a6c743

Browse files
Copilotserhalp
andcommitted
fix!: only serve dev static assets from publish dir as fallback
When using `@netlify/vite-plugin`, `vite dev` (or e.g. `astro dev`) was incorrectly serving files from the Netlify publish dir (e.g. `dist/`) instead of the source directory during vite dev. I can't think of any reason to ever augment the directories passed to `@netlify/dev`'s options with the default directory. In every known use case (`@netlify/vite-plugin`, `@netlify/nuxt`...), we pass a known set of directories that should be used to serve static assets in dev. It seems reasonable to use some reasonable defaults here for future use cases. This commit makes this change. Co-authored-by: serhalp <1377702+serhalp@users.noreply.github.com>
1 parent 0300389 commit 3a6c743

File tree

2 files changed

+99
-8
lines changed

2 files changed

+99
-8
lines changed

packages/dev/src/main.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,4 +1062,94 @@ describe('Handling requests', () => {
10621062
await fixture.destroy()
10631063
})
10641064
})
1065+
1066+
describe('Static file serving', () => {
1067+
test('Serves static files from given `staticFiles.directories` only', async () => {
1068+
const fixture = new Fixture()
1069+
.withFile(
1070+
'netlify.toml',
1071+
`[build]
1072+
publish = "dist"
1073+
`,
1074+
)
1075+
.withFile('dist/index.html', `from dist`)
1076+
.withFile('custom-static/app.css', `from custom-static`)
1077+
.withFile('another-static/script.js', `from another-static`)
1078+
const directory = await fixture.create()
1079+
1080+
const dev = new NetlifyDev({
1081+
projectRoot: directory,
1082+
edgeFunctions: {},
1083+
geolocation: {
1084+
enabled: false,
1085+
},
1086+
staticFiles: {
1087+
directories: [`${directory}/custom-static`, `${directory}/another-static`],
1088+
},
1089+
})
1090+
await dev.start()
1091+
1092+
const customStaticRes = await dev.handle(new Request('https://site.netlify/app.css'))
1093+
expect(await customStaticRes?.text()).toBe('from custom-static')
1094+
const anotherStaticRes = await dev.handle(new Request('https://site.netlify/script.js'))
1095+
expect(await anotherStaticRes?.text()).toBe('from another-static')
1096+
const publishDirRes = await dev.handle(new Request('https://site.netlify/index.html'))
1097+
expect(publishDirRes).toBeUndefined()
1098+
1099+
await dev.stop()
1100+
await fixture.destroy()
1101+
})
1102+
1103+
test('Falls back to publish directory when no custom directories provided', async () => {
1104+
const fixture = new Fixture()
1105+
.withFile(
1106+
'netlify.toml',
1107+
`[build]
1108+
publish = "public"
1109+
`,
1110+
)
1111+
.withFile('public/index.html', `from public`)
1112+
.withFile('public/style.css', `from public css`)
1113+
const directory = await fixture.create()
1114+
1115+
const dev = new NetlifyDev({
1116+
projectRoot: directory,
1117+
edgeFunctions: {},
1118+
geolocation: {
1119+
enabled: false,
1120+
},
1121+
})
1122+
await dev.start()
1123+
1124+
const publishDirRes1 = await dev.handle(new Request('https://site.netlify/index.html'))
1125+
expect(await publishDirRes1?.text()).toBe('from public')
1126+
const publishDirRes2 = await dev.handle(new Request('https://site.netlify/style.css'))
1127+
expect(await publishDirRes2?.text()).toBe('from public css')
1128+
1129+
await dev.stop()
1130+
await fixture.destroy()
1131+
})
1132+
1133+
test('Uses project root when no publish directory configured and no custom directories provided', async () => {
1134+
const fixture = new Fixture().withFile('index.html', `from root`).withFile('app.js', `from root js`)
1135+
const directory = await fixture.create()
1136+
1137+
const dev = new NetlifyDev({
1138+
projectRoot: directory,
1139+
edgeFunctions: {},
1140+
geolocation: {
1141+
enabled: false,
1142+
},
1143+
})
1144+
await dev.start()
1145+
1146+
const projectRootRes1 = await dev.handle(new Request('https://site.netlify/index.html'))
1147+
expect(await projectRootRes1?.text()).toBe('from root')
1148+
const projectRootRes2 = await dev.handle(new Request('https://site.netlify/app.js'))
1149+
expect(await projectRootRes2?.text()).toBe('from root js')
1150+
1151+
await dev.stop()
1152+
await fixture.destroy()
1153+
})
1154+
})
10651155
})

packages/dev/src/main.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ export interface Features {
123123
enabled?: boolean
124124

125125
/**
126-
* Additional list of directories where static files can be found. The
127-
* `publish` directory configured on your site will be used automatically.
126+
* List of directories where static files can be found. If not provided,
127+
* the `publish` directory configured on your Netlify project will be used automatically.
128128
*/
129129
directories?: string[]
130130
}
@@ -196,7 +196,7 @@ export class NetlifyDev {
196196
#serverAddress?: string | null
197197
#siteID?: string
198198
#staticHandler?: StaticHandler
199-
#staticHandlerAdditionalDirectories: string[]
199+
#staticHandlerDirectories?: string[]
200200

201201
constructor(options: NetlifyDevOptions) {
202202
if (options.apiURL) {
@@ -227,7 +227,7 @@ export class NetlifyDev {
227227
this.#logger = options.logger ?? globalThis.console
228228
this.#serverAddress = options.serverAddress
229229
this.#projectRoot = projectRoot
230-
this.#staticHandlerAdditionalDirectories = options.staticFiles?.directories ?? []
230+
this.#staticHandlerDirectories = options.staticFiles?.directories ?? undefined
231231
}
232232

233233
private getServerAddress(requestServerAddress?: string) {
@@ -582,11 +582,12 @@ export class NetlifyDev {
582582
}
583583

584584
if (this.#features.static) {
585+
// If custom static directories are provided (e.g., by `@netlify/vite-plugin` or `@netlify/nuxt`),
586+
// use those directories. Otherwise, use the build.publish directory from config.
587+
const directories = this.#staticHandlerDirectories ?? [this.#config?.config.build.publish ?? this.#projectRoot]
588+
585589
this.#staticHandler = new StaticHandler({
586-
directory: [
587-
this.#config?.config.build.publish ?? this.#projectRoot,
588-
...this.#staticHandlerAdditionalDirectories,
589-
],
590+
directory: directories,
590591
})
591592
}
592593

0 commit comments

Comments
 (0)