Skip to content

Commit

Permalink
[fs-detectors] Don't add "api/*" 404 route when only a middleware is …
Browse files Browse the repository at this point in the history
…present (vercel#8920)

This route should not be added when there are no explicit builds under
the "api" directory.

Fixes sveltejs/kit#6777.
  • Loading branch information
TooTallNate authored Nov 16, 2022
1 parent 2dae293 commit 896dd04
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
21 changes: 13 additions & 8 deletions packages/cli/test/unit/commands/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,12 @@ describe('build', () => {
expect(config).toMatchObject({
version: 3,
routes: [
{ src: '^/.*$', middlewarePath: 'middleware', continue: true },
{ handle: 'filesystem' },
{ src: '^/api(/.*)?$', status: 404 },
{
src: '^/.*$',
middlewarePath: 'middleware',
override: true,
continue: true,
},
{ handle: 'error' },
{ status: 404, src: '^(?!/api).*$', dest: '/404.html' },
],
Expand Down Expand Up @@ -546,9 +549,12 @@ describe('build', () => {
expect(config).toMatchObject({
version: 3,
routes: [
{ src: '^/.*$', middlewarePath: 'middleware', continue: true },
{ handle: 'filesystem' },
{ src: '^/api(/.*)?$', status: 404 },
{
src: '^/.*$',
middlewarePath: 'middleware',
override: true,
continue: true,
},
{ handle: 'error' },
{ status: 404, src: '^(?!/api).*$', dest: '/404.html' },
],
Expand Down Expand Up @@ -610,10 +616,9 @@ describe('build', () => {
{
src: '^\\/about(?:\\/((?:[^\\/#\\?]+?)(?:\\/(?:[^\\/#\\?]+?))*))?[\\/#\\?]?$|^\\/dashboard(?:\\/((?:[^\\/#\\?]+?)(?:\\/(?:[^\\/#\\?]+?))*))?[\\/#\\?]?$',
middlewarePath: 'middleware',
override: true,
continue: true,
},
{ handle: 'filesystem' },
{ src: '^/api(/.*)?$', status: 404 },
{ handle: 'error' },
{ status: 404, src: '^(?!/api).*$', dest: '/404.html' },
],
Expand Down
5 changes: 4 additions & 1 deletion packages/fs-detectors/src/detect-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,10 @@ function getRouteResult(

rewriteRoutes.push(...dynamicRoutes);

if (typeof ignoreRuntimes === 'undefined') {
const hasApiBuild = apiBuilders.find(builder => {
return builder.src?.startsWith('api/');
});
if (typeof ignoreRuntimes === 'undefined' && hasApiBuild) {
// This route is only necessary to hide the directory listing
// to avoid enumerating serverless function names.
// But it causes issues in `vc dev` for frameworks that handle
Expand Down
22 changes: 16 additions & 6 deletions packages/fs-detectors/test/unit.builds-and-routes-detector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2235,9 +2235,14 @@ describe('Test `detectBuilders` with `featHandleMiss=true`', () => {

it('no package.json + no build + root-level "middleware.js"', async () => {
const files = ['middleware.js', 'index.html', 'web/middleware.js'];
const { builders, errors } = await detectBuilders(files, null, {
featHandleMiss,
});
const { builders, rewriteRoutes, errors } = await detectBuilders(
files,
null,
{
featHandleMiss,
}
);
expect(rewriteRoutes).toHaveLength(0);
expect(builders![0].use).toBe('@vercel/node');
expect(builders![0].src).toBe('middleware.js');
expect(builders![0].config?.middleware).toEqual(true);
Expand All @@ -2249,9 +2254,14 @@ describe('Test `detectBuilders` with `featHandleMiss=true`', () => {

it('no package.json + no build + root-level "middleware.ts"', async () => {
const files = ['middleware.ts', 'index.html', 'web/middleware.js'];
const { builders, errors } = await detectBuilders(files, null, {
featHandleMiss,
});
const { builders, rewriteRoutes, errors } = await detectBuilders(
files,
null,
{
featHandleMiss,
}
);
expect(rewriteRoutes).toHaveLength(0);
expect(builders![0].use).toBe('@vercel/node');
expect(builders![0].src).toBe('middleware.ts');
expect(builders![0].config?.middleware).toEqual(true);
Expand Down

0 comments on commit 896dd04

Please sign in to comment.