Redirect all urls to trailing slash while avoiding a 301 redirect to a 404 page

Hey hey

One of the requirements of a site we're working on is to end all URL's with a trailing slash.

All URL's are currently redirected to their trailing slash version in middleware.
if (!context.route.path.endsWith('/')) {
context.redirect(301, context.route.path + '/')

When you visit /foo/bar you're 301 redirected to /foo/bar/ which is a 404 page.
The problem is this will show as:
curl -I
HTTP/2 301
location: /foo/bar/

We want all non trailing slashes to be redirected to their trailing slash versions as a 301.
so /contact-us = /contact-us/

I understand why this happens, it's before any requests to Prismic have been made, we don't know the page is a 404 yet.

Have you seen this before and have any ideas how to enforce trailing slash after fetching successful without updating every page view with the redirect inside the asyncdata?


Solved this by creating a global plugin that is only run after Prismic page api calls have been made/successful.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.