Skip to content

Commit

Permalink
fix: do not append traling slash to subresource urls
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Correa Casablanca <[email protected]>
  • Loading branch information
castarco committed Mar 19, 2024
1 parent ad57a02 commit 0abf851
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-snails-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/node": patch
---

fix: do not append traling slash to subresource urls
3 changes: 2 additions & 1 deletion packages/integrations/node/src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export function createStaticHandler(app: NodeApp, options: Options) {
}
break;
case 'always':
if (!hasSlash) {
// trailing slash is not added to "subresources"
if (!hasSlash && !urlPath.match(/.+\.[a-z]+$/i)) {
pathname = urlPath + '/' + (urlQuery ? '?' + urlQuery : '');
res.statusCode = 301;
res.setHeader('Location', pathname);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 { color: red; }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai';
import { after, before, describe, it, expect } from 'node:test';
import * as cheerio from 'cheerio';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';
Expand Down Expand Up @@ -76,6 +76,14 @@ describe('Trailing slash', () => {
expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
});

it('Does not add trailing slash to subresource urls', async () => {
const res = await fetch(`http://${server.host}:${server.port}/some-base/one.css`);
const css = await res.text();

expect(res.status).to.equal(200);
expect(css).to.equal('h1 { color: red; }\n');
})
});
describe('Without base', async () => {
before(async () => {
Expand Down Expand Up @@ -133,6 +141,14 @@ describe('Trailing slash', () => {
expect(res.status).to.equal(200);
expect($('h1').text()).to.equal('One');
});

it('Does not add trailing slash to subresource urls', async () => {
const res = await fetch(`http://${server.host}:${server.port}/one.css`);
const css = await res.text();

expect(res.status).to.equal(200);
expect(css).to.equal('h1 { color: red; }\n');
})
});
});
describe('Never', async () => {
Expand Down

0 comments on commit 0abf851

Please sign in to comment.