From fa3cd4f53cae0b39fc854460ba6f06d13e0fe084 Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Wed, 9 Mar 2022 11:14:08 -0800 Subject: [PATCH] improve preview reliability - fix broken tests --- .npmrc | 2 -- packages/astro/src/core/preview/index.ts | 5 +++++ packages/astro/test/preview-routing.test.js | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.npmrc b/.npmrc index 6e736d1958..5a877c7ddb 100644 --- a/.npmrc +++ b/.npmrc @@ -3,8 +3,6 @@ prefer-workspace-packages=true link-workspace-packages=true save-workspace-protocol=false # This prevents the examples to have the `workspace:` prefix -use-node-version=14.19.0 - # Rather than shamefully hoisting everything, just make problematic packages public public-hoist-pattern[]=autoprefixer public-hoist-pattern[]=astro diff --git a/packages/astro/src/core/preview/index.ts b/packages/astro/src/core/preview/index.ts index 377f3a6e9a..aeafb9bc2f 100644 --- a/packages/astro/src/core/preview/index.ts +++ b/packages/astro/src/core/preview/index.ts @@ -23,6 +23,8 @@ export interface PreviewServer { stop(): Promise; } +const HAS_FILE_EXTENSION_REGEXP = /^.*\.[^\\]+$/; + /** The primary dev action */ export default async function preview(config: AstroConfig, { logging }: PreviewOptions): Promise { const startServerTime = performance.now(); @@ -60,6 +62,9 @@ export default async function preview(config: AstroConfig, { logging }: PreviewO case hasTrailingSlash && trailingSlash == 'never' && !isRoot: sendError('Not Found (devOptions.trailingSlash is set to "never")'); return; + case !hasTrailingSlash && trailingSlash == 'always' && !isRoot && !HAS_FILE_EXTENSION_REGEXP.test(pathname): + sendError('Not Found (devOptions.trailingSlash is set to "always")'); + return; default: { // HACK: rewrite req.url so that sirv finds the file req.url = '/' + req.url?.replace(baseURL.pathname, '') diff --git a/packages/astro/test/preview-routing.test.js b/packages/astro/test/preview-routing.test.js index 4f382bd8b6..5eeea843e6 100644 --- a/packages/astro/test/preview-routing.test.js +++ b/packages/astro/test/preview-routing.test.js @@ -12,6 +12,9 @@ describe('Preview Routing', () => { before(async () => { fixture = await loadFixture({ projectRoot: './fixtures/with-subpath-no-trailing-slash/', + buildOptions: { + pageUrlFormat: 'directory', + }, devOptions: { trailingSlash: 'never', port: 4000,