-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: redirect trailing slashes on on-demand rendered pages (#12994)
Co-authored-by: ematipico <602478+ematipico@users.noreply.github.com> Co-authored-by: matthewp <361671+matthewp@users.noreply.github.com> Co-authored-by: sarah11918 <5098874+sarah11918@users.noreply.github.com> Co-authored-by: bluwy <34116392+bluwy@users.noreply.github.com>
- Loading branch information
1 parent
e621712
commit 5361755
Showing
17 changed files
with
434 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/internal-helpers': minor | ||
--- | ||
|
||
Adds `collapseDuplicateTrailingSlashes` function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Redirects trailing slashes for on-demand pages | ||
|
||
When the `trailingSlash` option is set to `always` or `never`, on-demand rendered pages will now redirect to the correct URL when the trailing slash doesn't match the configuration option. This was previously the case for static pages, but now works for on-demand pages as well. | ||
|
||
Now, it doesn't matter whether your visitor navigates to `/about/`, `/about`, or even `/about///`. In production, they'll always end up on the correct page. For GET requests, the redirect will be a 301 (permanent) redirect, and for all other request methods, it will be a 308 (permanent, and preserve the request method) redirect. | ||
|
||
In development, you'll see a helpful 404 page to alert you of a trailing slash mismatch so you can troubleshoot routes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Returns a more helpful 404 page in dev if there is a trailing slash mismatch between the route requested and the `trailingSlash` configuration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/astro/src/vite-plugin-astro-server/trailing-slash.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import type * as vite from 'vite'; | ||
import type { AstroSettings } from '../types/astro.js'; | ||
|
||
import { collapseDuplicateTrailingSlashes, hasFileExtension } from '@astrojs/internal-helpers/path'; | ||
import { trailingSlashMismatchTemplate } from '../template/4xx.js'; | ||
import { writeHtmlResponse, writeRedirectResponse } from './response.js'; | ||
|
||
export function trailingSlashMiddleware(settings: AstroSettings): vite.Connect.NextHandleFunction { | ||
const { trailingSlash } = settings.config; | ||
|
||
return function devTrailingSlash(req, res, next) { | ||
const url = req.url!; | ||
|
||
const destination = collapseDuplicateTrailingSlashes(url, true); | ||
if (url && destination !== url) { | ||
return writeRedirectResponse(res, 301, destination); | ||
} | ||
let pathname: string; | ||
try { | ||
pathname = decodeURI(new URL(url, 'http://localhost').pathname); | ||
} catch (e) { | ||
/* malformed uri */ | ||
return next(e); | ||
} | ||
if ( | ||
(trailingSlash === 'never' && pathname.endsWith('/') && pathname !== '/') || | ||
(trailingSlash === 'always' && !pathname.endsWith('/') && !hasFileExtension(pathname)) | ||
) { | ||
const html = trailingSlashMismatchTemplate(pathname, trailingSlash); | ||
return writeHtmlResponse(res, 404, html); | ||
} | ||
return next(); | ||
}; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/ssr-response/src/pages/another.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
--- | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>Hello {Astro.url}</h1> | ||
</body> | ||
</html> |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/ssr-response/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
--- | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>Hello /</h1> | ||
</body> | ||
</html> |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/ssr-response/src/pages/sub/path.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
--- | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>Hello {Astro.url}</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.