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\""
},
"dependencies": {
"@astrojs/internal-helpers": "workspace:*",
"send": "^1.1.0",
"server-destroy": "^1.0.1"
},

View file

@ -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);

View file

@ -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>

View file

@ -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 () => {

48
pnpm-lock.yaml generated
View file

@ -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