serve icon indicating copy to clipboard operation
serve copied to clipboard

Rewrites issue

Open ionutcirja opened this issue 5 years ago • 3 comments

Hello,

I have this configuration for my rewrites:

"rewrites": [ { "source": "/**", "destination": "/index.html" }, { "source": "/logout", "destination": "/logout.html" } ]

I'm trying to use index.html for every route but not for logout. I tried many options, but can't really make it work.

Thank you

ionutcirja avatar Nov 11 '20 15:11 ionutcirja

Same thing here. Rewrites works perfectly except when the URL points to index.html, when it starts generating weird redirects. I would say rewrites are really broken right now.

Directory Layout

index.html
foo.txt
serve.json

serve.json

{
  "rewrites": [
    { "source": "/foo/bar/:path*", "destination": "/:path" }
  ],
  "trailingSlash": true
}

Observed behavior

  • GET /foo/bar/foo.txt: 200 OK
  • GET /foo/bar/index.html: 301 Moved Permanently (whaat?), redirects to /foo/bar/index, then to /foo/bar, then to /foo/bar/.
  • GET /foo/bar/: 404 Not Found

Without trailingSlash, I get the 404 Not Found one step earlier, when hitting /foo/bar. But that one feels normal since it doesn't match any rewrite rule.

jcayzac avatar Nov 26 '20 11:11 jcayzac

@ionutcirja if the rewrite sources are non-overlapping, it seems to work. (Not sure if this helps in your case...) This worked for me as expected:

{
  "cleanUrls": false,
  "rewrites": [
    { "source": "logout", "destination": "/logout.html" },
    { "source": "app/**", "destination": "/index.html" }
  ]
}

Looking at the applyRewrites function, it's not really obvious how it works. (early out + loop + recursion...)

heidemn avatar Jun 03 '21 00:06 heidemn

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.

..in relation to your example, process the 'logout' rule before the catch-all.. and mark it terminal

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