Correct handle directory finds when using base in the Node adapter (#7076)
This commit is contained in:
parent
0fc026f63c
commit
781f558c40
3 changed files with 24 additions and 4 deletions
5
.changeset/afraid-suits-talk.md
Normal file
5
.changeset/afraid-suits-talk.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/node': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix redirects on directories when using base option
|
|
@ -55,7 +55,13 @@ export function createServer(
|
||||||
// File not found, forward to the SSR handler
|
// File not found, forward to the SSR handler
|
||||||
handler(req, res);
|
handler(req, res);
|
||||||
});
|
});
|
||||||
|
stream.on('directory', () => {
|
||||||
|
// On directory find, redirect to the trailing slash
|
||||||
|
const location = req.url + '/';
|
||||||
|
res.statusCode = 301
|
||||||
|
res.setHeader('Location', location);
|
||||||
|
res.end(location);
|
||||||
|
});
|
||||||
stream.on('file', () => {
|
stream.on('file', () => {
|
||||||
forwardError = true;
|
forwardError = true;
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,6 +12,7 @@ describe('Prerendering', () => {
|
||||||
before(async () => {
|
before(async () => {
|
||||||
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
|
||||||
fixture = await loadFixture({
|
fixture = await loadFixture({
|
||||||
|
base: '/some-base',
|
||||||
root: './fixtures/prerender/',
|
root: './fixtures/prerender/',
|
||||||
output: 'server',
|
output: 'server',
|
||||||
adapter: nodejs({ mode: 'standalone' }),
|
adapter: nodejs({ mode: 'standalone' }),
|
||||||
|
@ -32,7 +33,7 @@ describe('Prerendering', () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
it('Can render SSR route', async () => {
|
it('Can render SSR route', async () => {
|
||||||
const res = await fetch(`http://${server.host}:${server.port}/one`);
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/one`);
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ describe('Prerendering', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can render prerendered route', async () => {
|
it('Can render prerendered route', async () => {
|
||||||
const res = await fetch(`http://${server.host}:${server.port}/two`);
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two`);
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
|
@ -50,11 +51,19 @@ describe('Prerendering', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can render prerendered route with query params', async () => {
|
it('Can render prerendered route with query params', async () => {
|
||||||
const res = await fetch(`http://${server.host}:${server.port}/two?foo=bar`);
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two/?foo=bar`);
|
||||||
const html = await res.text();
|
const html = await res.text();
|
||||||
const $ = cheerio.load(html);
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
expect(res.status).to.equal(200);
|
expect(res.status).to.equal(200);
|
||||||
expect($('h1').text()).to.equal('Two');
|
expect($('h1').text()).to.equal('Two');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Omitting the trailing slash results in a redirect that includes the base', async () => {
|
||||||
|
const res = await fetch(`http://${server.host}:${server.port}/some-base/two`, {
|
||||||
|
redirect: 'manual'
|
||||||
|
});
|
||||||
|
expect(res.status).to.equal(301);
|
||||||
|
expect(res.headers.get('location')).to.equal('/some-base/two/');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue