Replies: 1 comment
-
Here is how we ended up approaching this:
window.SvelteView = import('@views/users/show.svelte')
<%= vite_typescript_tag(
"entrypoints/views/#{params[:controller]}/#{params[:action]}", # e.g. entrypoints/views/users/show
'application',
) %>
const componentImport = await window.SvelteView
mount(componentImport.default, {
target: document.getElementById('main')
}) While a little bit more boilerplate code, this has enabled us to achieve the following:
This is the approach we went with and it seems to be working well. But keen to hear any other approaches people have taken or how the above might be able to be improved. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm looking for advice on what the best way to support an application with 100s of routes is, each one needing it's own top level view file, but doing so in way that can be references at runtime without bundling all references?
Here is our current setup, frontend is vite/svelte (not SvelteKit), served through Rails application that renders a svelte layout file (which in turn imports application.js and contains a
<div id="main"></div>
).Our app maps URL paths to svelte files:
/admin/users
->views/admin/users/index.svelte
/admin/users/ID
->views/admin/users/show.svelte
etc
In our application.ts, we have the following:
This provides us a nice mapping at runtime:
Our application can then read the URL path, get the import from the mapping, and render it (in reality, a bit more complicated than below, but hopefully you get the idea).
Now all of this works great, and has served us well for years. However, we're reaching a point where the creation of the views mapping object is quite large (100s of routes). Not only does this result in a large initial download, but exposes our entire underlying app structure on every page, which is not ideal.
The solution I'm investigating at the moment it to make each view file a Vite entrypoint so it is compiled without needing to use the import glob, but then without the mapping, we're not able to work out the path on the frontend because the final file is hashed, and our frontend can't know that without again adding some sort of mapping in that exposes all routes. We need a solution that doesn't need to fetch/hardcode each frontend file into the source code.
So this is where we're at and wondering how others have been able to solve this issue.
Beta Was this translation helpful? Give feedback.
All reactions