Skip to content

Commit

Permalink
Fixes custom 404 pages in astro dev (withastro#3331)
Browse files Browse the repository at this point in the history
* Fixing pathname matching for custom 404 pages

* fixes custom 404 routes in dev

* refactor: removing node path dependency

* refactor: using core's path utils
  • Loading branch information
Tony Sullivan committed May 10, 2022
1 parent d04928e commit e22f736
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/healthy-oranges-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue preventing custom 404 pages in dev
3 changes: 2 additions & 1 deletion packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type http from 'http';
import type { AstroConfig, ManifestData } from '../@types/astro';
import type { RenderResponse, SSROptions } from '../core/render/dev/index';
import { debug, info, warn, error, LogOptions } from '../core/logger/core.js';
import { appendForwardSlash } from '../core/path.js';
import { getParamsAndProps, GetParamsAndPropsError } from '../core/render/core.js';
import { createRouteManifest, matchRoute } from '../core/routing/index.js';
import stripAnsi from 'strip-ansi';
Expand Down Expand Up @@ -166,7 +167,7 @@ async function handle500Response(

function getCustom404Route(config: AstroConfig, manifest: ManifestData) {
const relPages = resolvePages(config).href.replace(config.root.href, '');
return manifest.routes.find((r) => r.component === relPages + '404.astro');
return manifest.routes.find((r) => r.component === appendForwardSlash(relPages) + '404.astro');
}

function log404(logging: LogOptions, pathname: string) {
Expand Down
41 changes: 41 additions & 0 deletions packages/astro/test/custom-404.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Custom 404', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/custom-404/',
});
});

describe('dev', () => {
let devServer;
let $;

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

after(async () => {
await devServer.stop();
});

it('renders /', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
$ = cheerio.load(html);

expect($('h1').text()).to.equal('Home');
});

it('renders 404 for /a', async () => {
const html = await fixture.fetch('/a').then((res) => res.text());
$ = cheerio.load(html);

expect($('h1').text()).to.equal('Page not found');
expect($('p').text()).to.equal('/a/');
})
});
});
4 changes: 4 additions & 0 deletions packages/astro/test/fixtures/custom-404/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { defineConfig } from 'astro/config';

// https://astro.build/config
export default defineConfig({});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/custom-404/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/custom-404",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
12 changes: 12 additions & 0 deletions packages/astro/test/fixtures/custom-404/src/pages/404.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
---

<html lang="en">
<head>
<title>Not Found - Custom 404</title>
</head>
<body>
<h1>Page not found</h1>
<p>{Astro.canonicalURL.pathname}</p>
</body>
</html>
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/custom-404/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
---

<html lang="en">
<head>
<title>Custom 404</title>
</head>
<body>
<h1>Home</h1>
</body>
</html>
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit e22f736

Please sign in to comment.