diff options
author | 2025-02-12 10:34:32 +0000 | |
---|---|---|
committer | 2025-02-12 10:34:32 +0000 | |
commit | 23094a1f48d0dfb12c5866a3713f52106ef927dd (patch) | |
tree | 33c68bbe2c503450eb916eb3983dda3b7053ef0b | |
parent | 7d94b49870b9258e1ab242c5410cf6da20b5c78b (diff) | |
download | astro-23094a1f48d0dfb12c5866a3713f52106ef927dd.tar.gz astro-23094a1f48d0dfb12c5866a3713f52106ef927dd.tar.zst astro-23094a1f48d0dfb12c5866a3713f52106ef927dd.zip |
fix: use shared helper for file extensions (#13223)
* fix: use shared helper for file extensions
* Lock
-rw-r--r-- | .changeset/silent-guests-play.md | 5 | ||||
-rw-r--r-- | packages/integrations/node/package.json | 1 | ||||
-rw-r--r-- | packages/integrations/node/src/serve-static.ts | 5 | ||||
-rw-r--r-- | packages/integrations/node/test/fixtures/trailing-slash/src/assets/bitgeneva12.woff2 | bin | 0 -> 5196 bytes | |||
-rw-r--r-- | packages/integrations/node/test/fixtures/trailing-slash/src/pages/index.astro | 9 | ||||
-rw-r--r-- | packages/integrations/node/test/trailing-slash.test.js | 24 | ||||
-rw-r--r-- | pnpm-lock.yaml | 48 |
7 files changed, 59 insertions, 33 deletions
diff --git a/.changeset/silent-guests-play.md b/.changeset/silent-guests-play.md new file mode 100644 index 000000000..58fc05af7 --- /dev/null +++ b/.changeset/silent-guests-play.md @@ -0,0 +1,5 @@ +--- +'@astrojs/node': patch +--- + +Fixes a bug that caused incorrect redirects for static files with numbers in the file extension diff --git a/packages/integrations/node/package.json b/packages/integrations/node/package.json index 6cca2210f..ab64c3413 100644 --- a/packages/integrations/node/package.json +++ b/packages/integrations/node/package.json @@ -31,6 +31,7 @@ "test": "astro-scripts test \"test/**/*.test.js\"" }, "dependencies": { + "@astrojs/internal-helpers": "workspace:*", "send": "^1.1.0", "server-destroy": "^1.0.1" }, diff --git a/packages/integrations/node/src/serve-static.ts b/packages/integrations/node/src/serve-static.ts index c7ec56aae..3b1b1e9a7 100644 --- a/packages/integrations/node/src/serve-static.ts +++ b/packages/integrations/node/src/serve-static.ts @@ -5,9 +5,8 @@ import url from 'node:url'; import type { NodeApp } from 'astro/app/node'; import send from 'send'; import type { Options } from './types.js'; +import { hasFileExtension } from '@astrojs/internal-helpers/path'; -// check for a dot followed by a extension made up of lowercase characters -const isSubresourceRegex = /.+\.[a-z]+$/i; /** * Creates a Node.js http listener for static files and prerendered pages. @@ -56,7 +55,7 @@ export function createStaticHandler(app: NodeApp, options: Options) { } case 'always': { // trailing slash is not added to "subresources" - if (!hasSlash && !isSubresourceRegex.test(urlPath)) { + if (!hasSlash && !hasFileExtension(urlPath)) { pathname = urlPath + '/' + (urlQuery ? '?' + urlQuery : ''); res.statusCode = 301; res.setHeader('Location', pathname); diff --git a/packages/integrations/node/test/fixtures/trailing-slash/src/assets/bitgeneva12.woff2 b/packages/integrations/node/test/fixtures/trailing-slash/src/assets/bitgeneva12.woff2 Binary files differnew file mode 100644 index 000000000..7c5c2567e --- /dev/null +++ b/packages/integrations/node/test/fixtures/trailing-slash/src/assets/bitgeneva12.woff2 diff --git a/packages/integrations/node/test/fixtures/trailing-slash/src/pages/index.astro b/packages/integrations/node/test/fixtures/trailing-slash/src/pages/index.astro index a4c415519..495eb989b 100644 --- a/packages/integrations/node/test/fixtures/trailing-slash/src/pages/index.astro +++ b/packages/integrations/node/test/fixtures/trailing-slash/src/pages/index.astro @@ -6,3 +6,12 @@ <h1>Index</h1> </body> </html> +<style> + @font-face { + font-family: 'Geneva'; + src: url('../assets/bitgeneva12.woff2') format('woff2'); + } + h1 { + font-family: 'Geneva', sans-serif; + } +</style> diff --git a/packages/integrations/node/test/trailing-slash.test.js b/packages/integrations/node/test/trailing-slash.test.js index feed313ce..948f7e0fb 100644 --- a/packages/integrations/node/test/trailing-slash.test.js +++ b/packages/integrations/node/test/trailing-slash.test.js @@ -81,6 +81,17 @@ describe('Trailing slash', () => { assert.equal(res.status, 200); assert.equal(css, 'h1 { color: red; }\n'); }); + + it('Does not redirect requests for static assets with unusual filenames', async () => { + const res = await fetch( + `http://${server.host}:${server.port}/some-base/_astro/bitgeneva12.NY2V_gnX.woff2`, + { + redirect: 'manual', + }, + ); + + assert.equal(res.status, 200); + }); }); describe('Without base', async () => { before(async () => { @@ -143,12 +154,23 @@ describe('Trailing slash', () => { }); it('Does not add trailing slash to subresource urls', async () => { - const res = await fetch(`http://${server.host}:${server.port}/one.css`); + const res = await fetch(`http://${server.host}:${server.port}/one.css`, { redirect: 'manual' }); const css = await res.text(); assert.equal(res.status, 200); assert.equal(css, 'h1 { color: red; }\n'); }); + + it('Does not redirect requests for static assets with unusual filenames', async () => { + const res = await fetch( + `http://${server.host}:${server.port}/_astro/bitgeneva12.NY2V_gnX.woff2`, + { + redirect: 'manual', + }, + ); + + assert.equal(res.status, 200); + }); }); }); describe('Never', async () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b07bd2364..12f6981e3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -68,7 +68,7 @@ importers: version: link:../packages/integrations/mdx '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../packages/integrations/node '@benchmark/adapter': specifier: workspace:* version: link:packages/adapter @@ -327,7 +327,7 @@ importers: dependencies: '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../packages/integrations/node astro: specifier: ^5.2.5 version: link:../../packages/astro @@ -354,7 +354,7 @@ importers: dependencies: '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../packages/integrations/node '@astrojs/svelte': specifier: ^7.0.4 version: link:../../packages/integrations/svelte @@ -799,7 +799,7 @@ importers: version: link:../../../../db '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node '@astrojs/react': specifier: workspace:* version: link:../../../../integrations/react @@ -832,7 +832,7 @@ importers: version: link:../../../../db '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node '@astrojs/react': specifier: workspace:* version: link:../../../../integrations/react @@ -1068,7 +1068,7 @@ importers: dependencies: '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node astro: specifier: workspace:* version: link:../../.. @@ -1470,7 +1470,7 @@ importers: version: link:../../../../integrations/mdx '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node '@astrojs/react': specifier: workspace:* version: link:../../../../integrations/react @@ -1488,7 +1488,7 @@ importers: dependencies: '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node astro: specifier: workspace:* version: link:../../.. @@ -1583,7 +1583,7 @@ importers: dependencies: '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node '@astrojs/react': specifier: workspace:* version: link:../../../../integrations/react @@ -2439,7 +2439,7 @@ importers: dependencies: '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node astro: specifier: workspace:* version: link:../../.. @@ -2970,7 +2970,7 @@ importers: dependencies: '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node astro: specifier: workspace:* version: link:../../.. @@ -3865,7 +3865,7 @@ importers: dependencies: '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node astro: specifier: workspace:* version: link:../../.. @@ -4077,7 +4077,7 @@ importers: dependencies: '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node '@test/static-build-pkg': specifier: workspace:* version: link:../static-build/pkg @@ -4452,7 +4452,7 @@ importers: version: link:../../.. '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../integrations/node '@astrojs/react': specifier: workspace:* version: link:../../../../integrations/react @@ -5263,6 +5263,9 @@ importers: packages/integrations/node: dependencies: + '@astrojs/internal-helpers': + specifier: workspace:* + version: link:../../internal-helpers send: specifier: ^1.1.0 version: 1.1.0 @@ -5540,7 +5543,7 @@ importers: devDependencies: '@astrojs/node': specifier: ^9.0.0 - version: 9.0.2(astro@packages+astro) + version: link:../node astro: specifier: workspace:* version: link:../../astro @@ -6011,7 +6014,7 @@ importers: version: link:../../../../../db '@astrojs/node': specifier: ^9.0.2 - version: 9.0.2(astro@packages+astro) + version: link:../../../../node '@astrojs/web-vitals': specifier: workspace:* version: link:../../.. @@ -6314,11 +6317,6 @@ packages: prettier-plugin-astro: optional: true - '@astrojs/node@9.0.2': - resolution: {integrity: sha512-MFFYRa5yQEBegKrSUPMeKnjDMB4okTrkVRA40/mU3ADKrKY5VV3af0LS+NYkH9pFOvj/OsPbdeQVxQ0jI3f6aQ==} - peerDependencies: - astro: ^5.0.0 - '@astrojs/yaml2ts@0.2.1': resolution: {integrity: sha512-CBaNwDQJz20E5WxzQh4thLVfhB3JEEGz72wRA+oJp6fQR37QLAqXZJU0mHC+yqMOQ6oj0GfRPJrz6hjf+zm6zA==} @@ -12815,14 +12813,6 @@ snapshots: transitivePeerDependencies: - typescript - '@astrojs/node@9.0.2(astro@packages+astro)': - dependencies: - astro: link:packages/astro - send: 1.1.0 - server-destroy: 1.0.1 - transitivePeerDependencies: - - supports-color - '@astrojs/yaml2ts@0.2.1': dependencies: yaml: 2.5.1 |