vercel / serve-handler

The foundation of `serve`
https://npmjs.com/serve-handler
MIT License
574 stars 97 forks source link

Redirect all routes to a single page with some exception #71

Open johangirod opened 6 years ago

johangirod commented 6 years ago

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!

Fanzoo commented 4 years ago

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?

laeijebor commented 3 years ago

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"
            },

       ]
}

Hope this helps someone!

warren-bank commented 2 years ago

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: