0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-10 23:01:26 -05:00

fix: use shared helper for file extensions (#13223)

* fix: use shared helper for file extensions

* Lock
This commit is contained in:
Matt Kane 2025-02-12 10:34:32 +00:00 committed by GitHub
parent 7d94b49870
commit 23094a1f48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 59 additions and 33 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---
Fixes a bug that caused incorrect redirects for static files with numbers in the file extension

View file

@ -31,6 +31,7 @@
"test": "astro-scripts test \"test/**/*.test.js\"" "test": "astro-scripts test \"test/**/*.test.js\""
}, },
"dependencies": { "dependencies": {
"@astrojs/internal-helpers": "workspace:*",
"send": "^1.1.0", "send": "^1.1.0",
"server-destroy": "^1.0.1" "server-destroy": "^1.0.1"
}, },

View file

@ -5,9 +5,8 @@ import url from 'node:url';
import type { NodeApp } from 'astro/app/node'; import type { NodeApp } from 'astro/app/node';
import send from 'send'; import send from 'send';
import type { Options } from './types.js'; 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. * 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': { case 'always': {
// trailing slash is not added to "subresources" // trailing slash is not added to "subresources"
if (!hasSlash && !isSubresourceRegex.test(urlPath)) { if (!hasSlash && !hasFileExtension(urlPath)) {
pathname = urlPath + '/' + (urlQuery ? '?' + urlQuery : ''); pathname = urlPath + '/' + (urlQuery ? '?' + urlQuery : '');
res.statusCode = 301; res.statusCode = 301;
res.setHeader('Location', pathname); res.setHeader('Location', pathname);

View file

@ -6,3 +6,12 @@
<h1>Index</h1> <h1>Index</h1>
</body> </body>
</html> </html>
<style>
@font-face {
font-family: 'Geneva';
src: url('../assets/bitgeneva12.woff2') format('woff2');
}
h1 {
font-family: 'Geneva', sans-serif;
}
</style>

View file

@ -81,6 +81,17 @@ describe('Trailing slash', () => {
assert.equal(res.status, 200); assert.equal(res.status, 200);
assert.equal(css, 'h1 { color: red; }\n'); 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 () => { describe('Without base', async () => {
before(async () => { before(async () => {
@ -143,12 +154,23 @@ describe('Trailing slash', () => {
}); });
it('Does not add trailing slash to subresource urls', async () => { 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(); const css = await res.text();
assert.equal(res.status, 200); assert.equal(res.status, 200);
assert.equal(css, 'h1 { color: red; }\n'); 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 () => { describe('Never', async () => {

48
pnpm-lock.yaml generated
View file

@ -68,7 +68,7 @@ importers:
version: link:../packages/integrations/mdx version: link:../packages/integrations/mdx
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../packages/integrations/node
'@benchmark/adapter': '@benchmark/adapter':
specifier: workspace:* specifier: workspace:*
version: link:packages/adapter version: link:packages/adapter
@ -327,7 +327,7 @@ importers:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../packages/integrations/node
astro: astro:
specifier: ^5.2.5 specifier: ^5.2.5
version: link:../../packages/astro version: link:../../packages/astro
@ -354,7 +354,7 @@ importers:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../packages/integrations/node
'@astrojs/svelte': '@astrojs/svelte':
specifier: ^7.0.4 specifier: ^7.0.4
version: link:../../packages/integrations/svelte version: link:../../packages/integrations/svelte
@ -799,7 +799,7 @@ importers:
version: link:../../../../db version: link:../../../../db
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
'@astrojs/react': '@astrojs/react':
specifier: workspace:* specifier: workspace:*
version: link:../../../../integrations/react version: link:../../../../integrations/react
@ -832,7 +832,7 @@ importers:
version: link:../../../../db version: link:../../../../db
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
'@astrojs/react': '@astrojs/react':
specifier: workspace:* specifier: workspace:*
version: link:../../../../integrations/react version: link:../../../../integrations/react
@ -1068,7 +1068,7 @@ importers:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
astro: astro:
specifier: workspace:* specifier: workspace:*
version: link:../../.. version: link:../../..
@ -1470,7 +1470,7 @@ importers:
version: link:../../../../integrations/mdx version: link:../../../../integrations/mdx
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
'@astrojs/react': '@astrojs/react':
specifier: workspace:* specifier: workspace:*
version: link:../../../../integrations/react version: link:../../../../integrations/react
@ -1488,7 +1488,7 @@ importers:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
astro: astro:
specifier: workspace:* specifier: workspace:*
version: link:../../.. version: link:../../..
@ -1583,7 +1583,7 @@ importers:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
'@astrojs/react': '@astrojs/react':
specifier: workspace:* specifier: workspace:*
version: link:../../../../integrations/react version: link:../../../../integrations/react
@ -2439,7 +2439,7 @@ importers:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
astro: astro:
specifier: workspace:* specifier: workspace:*
version: link:../../.. version: link:../../..
@ -2970,7 +2970,7 @@ importers:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
astro: astro:
specifier: workspace:* specifier: workspace:*
version: link:../../.. version: link:../../..
@ -3865,7 +3865,7 @@ importers:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
astro: astro:
specifier: workspace:* specifier: workspace:*
version: link:../../.. version: link:../../..
@ -4077,7 +4077,7 @@ importers:
dependencies: dependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
'@test/static-build-pkg': '@test/static-build-pkg':
specifier: workspace:* specifier: workspace:*
version: link:../static-build/pkg version: link:../static-build/pkg
@ -4452,7 +4452,7 @@ importers:
version: link:../../.. version: link:../../..
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../integrations/node
'@astrojs/react': '@astrojs/react':
specifier: workspace:* specifier: workspace:*
version: link:../../../../integrations/react version: link:../../../../integrations/react
@ -5263,6 +5263,9 @@ importers:
packages/integrations/node: packages/integrations/node:
dependencies: dependencies:
'@astrojs/internal-helpers':
specifier: workspace:*
version: link:../../internal-helpers
send: send:
specifier: ^1.1.0 specifier: ^1.1.0
version: 1.1.0 version: 1.1.0
@ -5540,7 +5543,7 @@ importers:
devDependencies: devDependencies:
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.0 specifier: ^9.0.0
version: 9.0.2(astro@packages+astro) version: link:../node
astro: astro:
specifier: workspace:* specifier: workspace:*
version: link:../../astro version: link:../../astro
@ -6011,7 +6014,7 @@ importers:
version: link:../../../../../db version: link:../../../../../db
'@astrojs/node': '@astrojs/node':
specifier: ^9.0.2 specifier: ^9.0.2
version: 9.0.2(astro@packages+astro) version: link:../../../../node
'@astrojs/web-vitals': '@astrojs/web-vitals':
specifier: workspace:* specifier: workspace:*
version: link:../../.. version: link:../../..
@ -6314,11 +6317,6 @@ packages:
prettier-plugin-astro: prettier-plugin-astro:
optional: true optional: true
'@astrojs/node@9.0.2':
resolution: {integrity: sha512-MFFYRa5yQEBegKrSUPMeKnjDMB4okTrkVRA40/mU3ADKrKY5VV3af0LS+NYkH9pFOvj/OsPbdeQVxQ0jI3f6aQ==}
peerDependencies:
astro: ^5.0.0
'@astrojs/yaml2ts@0.2.1': '@astrojs/yaml2ts@0.2.1':
resolution: {integrity: sha512-CBaNwDQJz20E5WxzQh4thLVfhB3JEEGz72wRA+oJp6fQR37QLAqXZJU0mHC+yqMOQ6oj0GfRPJrz6hjf+zm6zA==} resolution: {integrity: sha512-CBaNwDQJz20E5WxzQh4thLVfhB3JEEGz72wRA+oJp6fQR37QLAqXZJU0mHC+yqMOQ6oj0GfRPJrz6hjf+zm6zA==}
@ -12815,14 +12813,6 @@ snapshots:
transitivePeerDependencies: transitivePeerDependencies:
- typescript - 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': '@astrojs/yaml2ts@0.2.1':
dependencies: dependencies:
yaml: 2.5.1 yaml: 2.5.1