menu

ZEIT

Our mission is to make cloud computing as easy and accessible as mobile computing. You can find our Next.js community here.

Channels
Team

Static Routing Configuration Issues

January 11, 2019 at 1:16pm

Static Routing Configuration Issues

January 11, 2019 at 1:16pm

Hi All,

I'm trying to push a static site and having routing issues.

I normally use Surge for this which is really nicely configured (default is index.html, with a catch-all 200.html for any url that isn't a file in the build -- e.g. "/something/someData").

My static build has some .js, .map and .html files. I want all urls (catch all) to point to the index.html, except from actual build files (JS etc..). I've tried many versions of the config below, and nothing seems to be routing the way I want it aside from the default "/" route.

A route like "/something/someData" simply doesn't work and gives me a `404`.

What should my configuration look like for this? Is there a problem with Now Static builds atm?

{
"version": 2,
"builds": [
{ "src": "*.js", "use": "@now/static" },
{ "src": "*.map", "use": "@now/static" },
{ "src": "*.html", "use": "@now/static" }
],
"routes": [
{ "src": "/(.*)", "dest": "/index.html"},
],
}

January 11, 2019 at 1:17pm

Note, If I have to, I'm also fine defining all routes in the config as well -- but I'd prefer some kind of catch all method.

  • reply
  • like
@paulogdm
{"src": "^/(.*)", "dest": "/index.html"}
Edited
  • reply
  • like

That route doesn't seem to be catching anything, I try any extension and get a 404 still, thoughts?

  • reply
  • like

It seems with the static system that almost no routes are getting caught actually, even very specific ones..

  • reply
  • like

I got it!

Okay here is what confused me:

I was running now dist i.e. my build folder was dist

However my now.json file was outside my dist folder and thus any changes I made or ever made were not getting recorded.

You should maybe have a little note somewhere saying using /now.json file here... or some warning. I had no idea it wasn't picking up the json file.

Anyway, solved!

  • reply
  • like

You can always check the source in deployment.now.sh/_src to see if there is a now.json there. I will relay your feedback.

  • reply
  • like
{"src": "^/(.*)", "dest": "/index.html"}

That should probably be documented somewhere more prominently, it's the (not so obvious) solution to what I would imagine is a very common problem with the popularity of client-side routing.

  • reply
  • like
like-fill
1
  • reply
  • like

From my experience this still is only a partial solution, as it requires hard-coding all other valid routes at a higher priority (otherwise all requests for CSS/JS/images/etc are also rewritten to index.html). It would be nice to just have something similar to nginx's try_files or Netlify's _redirects

Edited
  • reply
  • like

We are working on the docs about this as we speak. Also, they are open source, feel free to do a PR if you think it is a quick addition.

like-fill
1
  • reply
  • like

From my experience this still is only a partial solution, as it requires hard-coding all other valid routes at a higher priority (otherwise all requests for CSS/JS/images/etc are also rewritten to index.html). It would be nice to just have something similar to nginx's try_files or Netlify's _redirects

Agree and we are working on options. Custom 404 is also on the works.

  • reply
  • like