Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.

withoutTrailingSlash #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion components/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Link from 'next/link'

const links = [
{ href: 'https://zeit.co/now', label: 'ZEIT' },
{ href: 'https://github.com/zeit/next.js', label: 'GitHub' },
{ href: '/about/', label: 'About' },
].map(link => {
link.key = `nav-link-${link.href}-${link.label}`
return link
Expand Down
24 changes: 24 additions & 0 deletions pages/_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Error from "next/error";
import Router from "next/router";

export default Error;

Error.getInitialProps = ({ res, err, asPath }) => {
const statusCode = res ? res.statusCode : err ? err.statusCode : 404;

if (statusCode && statusCode === 404) {
if (asPath.match(/\/$/)) {
const withoutTrailingSlash = asPath.substr(0, asPath.length - 1);
if (res) {
res.writeHead(302, {
Location: withoutTrailingSlash
});
res.end();
} else {
Router.push(withoutTrailingSlash);
}
}
}

return { statusCode };
};