Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

index.js in root of routes creates incorrect server route mapping #624

Closed
antony opened this issue Apr 9, 2019 · 5 comments · Fixed by #676
Closed

index.js in root of routes creates incorrect server route mapping #624

antony opened this issue Apr 9, 2019 · 5 comments · Fixed by #676

Comments

@antony
Copy link
Member

antony commented Apr 9, 2019

I'm trying to redirect the / route in a sapper project, by creating a server route as index.js in the root of /routes.

The structure of this would look like:

src/
├── routes/
|       └── index.js
...

Upon doing so, / returns a 404 and the generated route in server.js is:

    server_routes: [
        {
            // index.js
            pattern: /^\/index$/,
            handlers: __route_index,
            params: () => ({})
        }
    ]

I would expect the generated route pattern to be /^\/$/ instead of /^\/index$/.

@artemjackson
Copy link
Contributor

As a workaround I can advice you to put all api related routes under routes/api folder which is not affected by the issue

@antony
Copy link
Member Author

antony commented Apr 9, 2019

@artemjackson thanks - it's not an API route though. I want to redirect my / route.

I'm using an empty svelte component as a workaround, which does a redirect in its preload.

@artemjackson
Copy link
Contributor

it's not an API route though.

Well, let me clarify something

In a sapper app routes with the *.js extension are treated as server-side routes.
As I may suppose server side is not designed for redirects.

Routes with the *.html/*.svelte extension are treated as client side routes.
The client side routes indeed are designed for redirects.

So you are doing right by placing a redirect in a client-side route preload

@antony
Copy link
Member Author

antony commented Apr 9, 2019

@artemjackson Yes that's right, *.js is a server route, but it's simply a polka handler, so it can be used for pretty much whatever you want, from redirects, all the way to rendering content.

preload in a Svelte page component is another way to do this. preload as I'm sure you are aware, only runs on the server, so it too can be used for server-side redirects, or pre-loading content. Both are correct ways, but for this particular use-case, redirecting immediately inside the handler without having to pass control to a component is the preferred way.

None of this is in dispute, however. This bug relates to the way that Sapper's routing mechanism is interpreting a file in the root of the routes directory. It is incorrectly interpreting /index.js as a route called /index when the route should in fact be created as /.

@artemjackson
Copy link
Contributor

I finally got the point :)

Thanks for use-case clarification

mrkishi added a commit to mrkishi/sapper that referenced this issue May 5, 2019
Fixes sveltejs#624.

Treats both page and server routes similarly in regards to indexes and
trailing slashes.

It's a breaking change: `routes/page.ext.{svelte,html}` will no longer
accept trailing slashes, and `routes/server.js` will. We need a proper
way to configure clean urls behavior.
mrkishi added a commit to mrkishi/sapper that referenced this issue May 5, 2019
Fixes sveltejs#624.

Treats both page and server routes similarly in regards to indexes and
trailing slashes.

It's a breaking change: `routes/page.ext.{svelte,html}` will no longer
accept trailing slashes, and `routes/server.js` will. We need a proper
way to configure clean urls behavior.
mrkishi added a commit to mrkishi/sapper that referenced this issue May 5, 2019
Fixes sveltejs#624.

Treats both page and server routes similarly in regards to indexes and
trailing slashes.

It's a breaking change: `routes/page.ext.{svelte,html}` will no longer
accept trailing slashes, and `routes/server.js` will. We need a proper
way to configure clean urls behavior.
mrkishi added a commit to mrkishi/sapper that referenced this issue May 5, 2019
Fixes sveltejs#624.

Treats both page and server routes similarly in regards to indexes and
trailing slashes.

It's a breaking change: `routes/page.ext.{svelte,html}` will no longer
accept trailing slashes, and `routes/server.js` will. We need a proper
way to configure clean urls behavior.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants