Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(error pages): account for trailingSlash #9126

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-pandas-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where error pages were not shown when trailingSlash was set to "always".
3 changes: 2 additions & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ export class App {
request: Request,
{ status, response: originalResponse, skipMiddleware = false }: RenderErrorOptions
): Promise<Response> {
const errorRouteData = matchRoute('/' + status, this.#manifestData);
const errorRoutePath = `/${status}${this.#manifest.trailingSlash === "always" ? '/' : ''}`;
const errorRouteData = matchRoute(errorRoutePath, this.#manifestData);
const url = new URL(request.url);
if (errorRouteData) {
if (errorRouteData.prerender) {
Expand Down
45 changes: 0 additions & 45 deletions packages/astro/test/custom-404-server.test.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Custom 404', () => {

before(async () => {
fixture = await loadFixture({
root: './fixtures/custom-404/',
root: './fixtures/custom-404-static/',
site: 'http://example.com',
});
});
Expand Down Expand Up @@ -42,4 +42,15 @@ describe('Custom 404', () => {
expect($('p').text()).to.equal('/a');
});
});

describe('build', () => {
before(async () => {
await fixture.build();
})

it('builds to 404.html', async () => {
const html = await fixture.readFile('/404.html');
expect(html).to.be.ok;
});
});
});

This file was deleted.

8 changes: 0 additions & 8 deletions packages/astro/test/fixtures/custom-404-server/package.json

This file was deleted.

This file was deleted.

13 changes: 0 additions & 13 deletions packages/astro/test/fixtures/custom-404/src/pages/404.astro

This file was deleted.

11 changes: 0 additions & 11 deletions packages/astro/test/fixtures/custom-404/src/pages/index.astro

This file was deleted.

8 changes: 0 additions & 8 deletions packages/astro/test/fixtures/status-code/package.json

This file was deleted.

8 changes: 0 additions & 8 deletions packages/astro/test/fixtures/status-code/src/pages/404.astro

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import testAdapter from './test-adapter.js';
import * as cheerio from 'cheerio';

describe('404 and 500 pages', () => {
/** @type {import('./test-utils').Fixture} */
/** @type {import('./test-utils.js').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-api-route-custom-404/',
root: './fixtures/ssr-error-pages/',
output: 'server',
adapter: testAdapter(),
// test suite was authored when inlineStylesheets defaulted to never
Expand All @@ -18,8 +18,9 @@ describe('404 and 500 pages', () => {
});

describe('Development', () => {
/** @type {import('./test-utils').DevServer} */
/** @type {import('./test-utils.js').DevServer} */
let devServer;

before(async () => {
devServer = await fixture.startDevServer();
});
Expand All @@ -39,12 +40,15 @@ describe('404 and 500 pages', () => {
});

describe('Production', () => {
/** @type {import('./test-utils.js').App} */
let app;

before(async () => {
await fixture.build({});
app = await fixture.loadTestAdapterApp();
});

it('404 page returned when a route does not match', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/some/fake/route');
const response = await app.render(request);
expect(response.status).to.equal(404);
Expand All @@ -54,29 +58,26 @@ describe('404 and 500 pages', () => {
});

it('404 page returned when a route does not match and passing routeData', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/some/fake/route');
const routeData = app.match(request);
const response = await app.render(request, routeData);
const response = await app.render(request, { routeData });
expect(response.status).to.equal(404);
const html = await response.text();
const $ = cheerio.load(html);
expect($('h1').text()).to.equal('Something went horribly wrong!');
});

it('404 page returned when a route does not match and imports are included', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/blog/fake/route');
const routeData = app.match(request);
const response = await app.render(request, routeData);
const response = await app.render(request, { routeData });
expect(response.status).to.equal(404);
const html = await response.text();
const $ = cheerio.load(html);
expect($('head link')).to.have.a.lengthOf(1);
});

it('404 page returned when there is an 404 response returned from route', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/causes-404');
const response = await app.render(request);
expect(response.status).to.equal(404);
Expand All @@ -86,7 +87,6 @@ describe('404 and 500 pages', () => {
});

it('500 page returned when there is an error', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/causes-error');
const response = await app.render(request);
expect(response.status).to.equal(500);
Expand All @@ -96,7 +96,6 @@ describe('404 and 500 pages', () => {
});

it('Returns 404 when hitting an API route with the wrong method', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/api/route', {
method: 'PUT',
});
Expand All @@ -108,3 +107,54 @@ describe('404 and 500 pages', () => {
});
});
});

describe('trailing slashes for error pages', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-error-pages/',
output: 'server',
adapter: testAdapter(),
trailingSlash: 'always'
});
});

describe('Development', () => {
/** @type {import('./test-utils.js').DevServer} */
let devServer;

before(async () => {
devServer = await fixture.startDevServer();
});

it('renders 404 page when a route does not match the request', async () => {
const response = await fixture.fetch('/ashbfjkasn');
expect(response).to.deep.include({ status: 404 });
const html = await response.text();
expect(html).to.not.be.empty;
const $ = cheerio.load(html);
expect($('h1').text()).to.equal(`Something went horribly wrong!`);
});
});

describe('Production', () => {
/** @type {import('./test-utils.js').App} */
let app;

before(async () => {
await fixture.build({});
app = await fixture.loadTestAdapterApp();
});

it('renders 404 page when a route does not match the request', async () => {
const response = await app.render(new Request('http://example.com/ajksalscla'));
expect(response).to.deep.include({ status: 404 });
const html = await response.text();
expect(html).to.not.be.empty;
const $ = cheerio.load(html);
expect($('h1').text()).to.equal('Something went horribly wrong!');
})
});
});
19 changes: 0 additions & 19 deletions packages/astro/test/status-page.test.js

This file was deleted.

26 changes: 7 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading