[v3] Incorrect URLs in production build
I'm building an app with Electron and recently migrated to v3. After making sure routify works as expected in development I tried out the production build and noticed a couple of issues related to how URLs are handled.
Incorrect startup URL
First, the starting URL in $activeRoute.url defaults to /D:/Documents/VSCode/Projects/scrapbook/dist/win-unpacked/resources/app.asar/packages/renderer/dist/index.html instead of simply / causing an instant 404.
Incorrect resource requests
Next, when navigating the second time (after clearing the initial 404) I get a bunch of net::ERR_FILE_NOT_FOUND errors. From what I can tell it's trying to load components that aren't even used in a certain route.
For example: I have a component AuthorSearch.svelte which is requested when navigating to /series/index by calling GET file:///D:/series/AuthorSearch.ef141015.js even though it's used in /author/index.
Looking at the Sources tab all files are in the same folder (same one as the starting URL):
- Electron Isolated Context
- D:
- path/to/project/dist/.../app.asar/packages/renderer/dist
- index.html
- AuthorInfoDialog.2200b834.js
- AuthorSearch.ef141015.js
- ...
- author
- index <- (Not sure what this is except for an empty file)
- book
- index
- series
- index
- path/to/project/dist/.../app.asar/packages/renderer/dist
- D:
Despite the errors, navigating to a different route and then retrying the erroneous route causes it to load correctly producing no further errors.
Incorrect history
Lastly, when using history.back() from e.g. /book/[id] it causes the route to change to /D:/book/index resulting in a 404.
Config
- I'm using hash based routing
- The same setup worked with v2
- It only breaks in production mode
Version
I'm using routify v3.0.0-next.82
Edit: Here's a repo reproducing the issue.
Thanks for reporting this. Would it be possible to fake the initial window.location?
I just took a look at Router.svelte and couldn't find the config prop. Does v3 currently even support hash routing? If not, is it possible to make it work with Electron using the urlRewrite prop?
Edit: Found a workaround using the urlRewrite prop. This fixes all the aforementioned issues:
const urlRewrite: RoutifyRuntimeOptions['urlRewrite'] = {
toInternal: url => {
const hashIndex = url.indexOf('#');
return hashIndex !== -1 ? url.slice(hashIndex + 1) : '/';
},
toExternal: url => `#${url}`
};
And pass it to the Router instance <Router {routes} {urlRewrite} />
The only other thing I noticed is if you use redirects you need to prefix them with # e.g.
<script context="module" lang="ts">
const load: RoutifyLoad = () => ({
redirect: '#/book/index'
});
export { load };
</script>
So I guess it really does come down to v3 not officially supporting hash routing as of right now.