diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index cf4f9a412..ac70ec337 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -74,10 +74,7 @@ export const AstroConfigSchema = z.object({ .url() .optional() .transform((val) => (val ? appendForwardSlash(val) : val)), - base: z - .string() - .optional() - .default(ASTRO_CONFIG_DEFAULTS.base), + base: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.base), trailingSlash: z .union([z.literal('always'), z.literal('never'), z.literal('ignore')]) .optional() @@ -326,11 +323,11 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: URL) { config.build.client = new URL('./dist/client/', config.outDir); } const trimmedBase = trimSlashes(config.base); - if(trimmedBase.length && config.trailingSlash === 'never') { - config.base = prependForwardSlash(trimmedBase); - } else { - config.base = prependForwardSlash(appendForwardSlash(trimmedBase)); - } + if (trimmedBase.length && config.trailingSlash === 'never') { + config.base = prependForwardSlash(trimmedBase); + } else { + config.base = prependForwardSlash(appendForwardSlash(trimmedBase)); + } return config; }); diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index 52dbef8f0..fda9b7e58 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -61,7 +61,11 @@ function getParts(part: string, file: string) { return result; } -function getPattern(segments: RoutePart[][], base: string, addTrailingSlash: AstroConfig['trailingSlash']) { +function getPattern( + segments: RoutePart[][], + base: string, + addTrailingSlash: AstroConfig['trailingSlash'] +) { const pathname = segments .map((segment) => { if (segment.length === 1 && segment[0].spread) { @@ -94,7 +98,7 @@ function getPattern(segments: RoutePart[][], base: string, addTrailingSlash: Ast const trailing = addTrailingSlash && segments.length ? getTrailingSlashPattern(addTrailingSlash) : '$'; let initial = '\\/'; - if(addTrailingSlash === 'never' && base !== '/') { + if (addTrailingSlash === 'never' && base !== '/') { initial = ''; } return new RegExp(`^${pathname || initial}${trailing}`); diff --git a/packages/astro/src/vite-plugin-astro-server/request.ts b/packages/astro/src/vite-plugin-astro-server/request.ts index b0480f98f..1a144de5f 100644 --- a/packages/astro/src/vite-plugin-astro-server/request.ts +++ b/packages/astro/src/vite-plugin-astro-server/request.ts @@ -27,7 +27,7 @@ export async function handleRequest( const url = new URL(origin + req.url); let pathname: string; - if(config.trailingSlash === 'never' && !req.url) { + if (config.trailingSlash === 'never' && !req.url) { pathname = ''; } else { pathname = decodeURI(url.pathname); diff --git a/packages/astro/test/units/dev/base.test.js b/packages/astro/test/units/dev/base.test.js index 503b41002..d3dd94341 100644 --- a/packages/astro/test/units/dev/base.test.js +++ b/packages/astro/test/units/dev/base.test.js @@ -9,97 +9,121 @@ describe('base configuration', () => { describe('with trailingSlash: "never"', () => { describe('index route', () => { it('Requests that include a trailing slash 404', async () => { - const fs = createFs({ - '/src/pages/index.astro': `

testing

`, - }, root); - - await runInContainer({ - fs, - root, - userConfig: { - base: '/docs', - trailingSlash: 'never', + const fs = createFs( + { + '/src/pages/index.astro': `

testing

`, }, - }, async (container) => { - const { req, res, done } = createRequestAndResponse({ - method: 'GET', - url: '/docs/', - }); - container.handle(req, res); - await done; - expect(res.statusCode).to.equal(404); - }); + root + ); + + await runInContainer( + { + fs, + root, + userConfig: { + base: '/docs', + trailingSlash: 'never', + }, + }, + async (container) => { + const { req, res, done } = createRequestAndResponse({ + method: 'GET', + url: '/docs/', + }); + container.handle(req, res); + await done; + expect(res.statusCode).to.equal(404); + } + ); }); - + it('Requests that exclude a trailing slash 200', async () => { - const fs = createFs({ - '/src/pages/index.astro': `

testing

`, - }, root); - - await runInContainer({ - fs, - root, - userConfig: { - base: '/docs', - trailingSlash: 'never', + const fs = createFs( + { + '/src/pages/index.astro': `

testing

`, }, - }, async (container) => { - const { req, res, done } = createRequestAndResponse({ - method: 'GET', - url: '/docs', - }); - container.handle(req, res); - await done; - expect(res.statusCode).to.equal(200); - }); + root + ); + + await runInContainer( + { + fs, + root, + userConfig: { + base: '/docs', + trailingSlash: 'never', + }, + }, + async (container) => { + const { req, res, done } = createRequestAndResponse({ + method: 'GET', + url: '/docs', + }); + container.handle(req, res); + await done; + expect(res.statusCode).to.equal(200); + } + ); }); }); describe('sub route', () => { it('Requests that include a trailing slash 404', async () => { - const fs = createFs({ - '/src/pages/sub/index.astro': `

testing

`, - }, root); - - await runInContainer({ - fs, - root, - userConfig: { - base: '/docs', - trailingSlash: 'never', + const fs = createFs( + { + '/src/pages/sub/index.astro': `

testing

`, }, - }, async (container) => { - const { req, res, done } = createRequestAndResponse({ - method: 'GET', - url: '/docs/sub/', - }); - container.handle(req, res); - await done; - expect(res.statusCode).to.equal(404); - }); + root + ); + + await runInContainer( + { + fs, + root, + userConfig: { + base: '/docs', + trailingSlash: 'never', + }, + }, + async (container) => { + const { req, res, done } = createRequestAndResponse({ + method: 'GET', + url: '/docs/sub/', + }); + container.handle(req, res); + await done; + expect(res.statusCode).to.equal(404); + } + ); }); - + it('Requests that exclude a trailing slash 200', async () => { - const fs = createFs({ - '/src/pages/sub/index.astro': `

testing

`, - }, root); - - await runInContainer({ - fs, - root, - userConfig: { - base: '/docs', - trailingSlash: 'never', + const fs = createFs( + { + '/src/pages/sub/index.astro': `

testing

`, }, - }, async (container) => { - const { req, res, done } = createRequestAndResponse({ - method: 'GET', - url: '/docs/sub', - }); - container.handle(req, res); - await done; - expect(res.statusCode).to.equal(200); - }); + root + ); + + await runInContainer( + { + fs, + root, + userConfig: { + base: '/docs', + trailingSlash: 'never', + }, + }, + async (container) => { + const { req, res, done } = createRequestAndResponse({ + method: 'GET', + url: '/docs/sub', + }); + container.handle(req, res); + await done; + expect(res.statusCode).to.equal(200); + } + ); }); }); }); diff --git a/packages/astro/test/units/routing/manifest.test.js b/packages/astro/test/units/routing/manifest.test.js index b2ec5d503..05789d752 100644 --- a/packages/astro/test/units/routing/manifest.test.js +++ b/packages/astro/test/units/routing/manifest.test.js @@ -9,17 +9,23 @@ const root = new URL('../../fixtures/alias/', import.meta.url); describe('routing - createRouteManifest', () => { it('using trailingSlash: "never" does not match the index route when it contains a trailing slash', async () => { - const fs = createFs({ - '/src/pages/index.astro': `

test

`, - }, root); - const settings = await createDefaultDevSettings({ - base: '/search', - trailingSlash: 'never' - }, root); + const fs = createFs( + { + '/src/pages/index.astro': `

test

`, + }, + root + ); + const settings = await createDefaultDevSettings( + { + base: '/search', + trailingSlash: 'never', + }, + root + ); const manifest = createRouteManifest({ cwd: fileURLToPath(root), settings, - fsMod: fs + fsMod: fs, }); const [{ pattern }] = manifest.routes; expect(pattern.test('')).to.equal(true);