1
1
mirror of https://github.com/primer/css.git synced 2024-11-28 04:43:05 +03:00

docs: do one thing well in redirectTrailingSlash()

This commit is contained in:
Shawn Allen 2019-02-19 11:22:36 -08:00
parent 0988e6ecdf
commit 1f8929550f

View File

@ -1,5 +1,4 @@
import Router from 'next/router' import Router from 'next/router'
import Error from 'next/error'
/** /**
* Export this as your default from a page, and it'll redirect both server- * Export this as your default from a page, and it'll redirect both server-
@ -31,14 +30,11 @@ export default function redirect(uri, status = 303) {
} }
export function redirectTrailingSlash(context, status = 301) { export function redirectTrailingSlash(context, status = 301) {
const {req, res} = context const {req: {url}, res} = context
if (req.url.endsWith('/')) { if (url.endsWith('/')) {
const {url} = req
const withoutSlash = url.substr(0, url.length - 1) const withoutSlash = url.substr(0, url.length - 1)
res.writeHead(status, {Location: withoutSlash}) res.writeHead(status, {Location: withoutSlash})
res.end() res.end()
return true return true
} else {
return Error.getInitialProps(context)
} }
} }