serve-handler icon indicating copy to clipboard operation
serve-handler copied to clipboard

Redirect all routes to a single page with some exception

Open johangirod opened this issue 6 years ago • 3 comments

In a SPA settings, I'm prerendering the some landing pages for SEO and perf. But I can't figure out the corresponding rewrites rules.

So far, I've tried:

{
	"rewrites": [
		{ "source": "", "destination": "prerender/landing.html" },
		{
			"source": "/landing-2",
			"destination": "prerender/landing-2.html"
		},
		{ "source": "**", "destination": "index.html" }
	],
...
}

This doesn't work because apparently, all the rewrites rules are applied to the path (instead of stopping after the first match).

The problem is that I can't seem to find a way to negate the two prerendered path in the last rule, using minimatch.

What would be the proper way to handle this use case ? Is it even possible ?

Thanks!

johangirod avatar Oct 17 '18 16:10 johangirod

I am having this same issue. I'd like serve to rewrite all routes to a single page except a specific route (or sub-routes). Is there any way to accomplish this?

Fanzoo avatar Jan 23 '20 18:01 Fanzoo

For anyone else finding this issue - I had some success for my use case for making apple-app-site-association accessible (rewrites all routes for a react page using serve-handler, except for specified routes):

{
      rewrites: [
            {
                source: "/**!(apple-app-site-association)", destination: "/index.html"
            },
            {
                source: "/**/**", destination: "/index.html"
            },
            {
                source: "/apple-app-site-association",
                destination: "/apple-app-site-association.json"
            },

       ]
}
  • First entry rewrites all route endpoints except /apple-app-site-association to /index.html
  • Second rewrites everything in nested routes to /index.html
  • (Third one rewrites apple-app-site-association to a json file of the same name)

Hope this helps someone!

laeijebor avatar Mar 08 '21 23:03 laeijebor

shameless self-promotion alert:
this feature is available in my @warren-bank/serve fork of serve

both redirects and rewrites support an optional boolean terminal attribute, which will prevent any additional rules from being processed.

something else to bear in mind:

  • the --single cli option in this fork is handled differently
    • upstream serve prepends a redirect that matches all request paths and redirects them to '/index.html'
    • this fork only redirects pages that would otherwise return a 404 status code

warren-bank avatar Feb 16 '22 01:02 warren-bank