Open dayton-outar opened 1 year ago
👋 Hi, I was just working with this today and I managed to fix the infinite reloading issue, I'll summarize below:
The problem is that if you configure the base
path SvelteKit will get sad if you try to load the app outside that base and redirect you in a loop. By configuring base
the URLs to the JS are correct but when you try to embed the content of the built index.html page at /
(ie the WordPress site root) it goes into a redirect loop because it expects to load from /wp-content/plugins/sveltekit-wordpress-plugin/sveltekit/build/
At the entrypoint in start.js
SvelteKit runs client.goto(location.href, { replaceState: true });
which triggers location.href = url.href;
causing the loop. That's why I asked if it was possible to skip the router. :sweat_smile: I tried data-sveltekit-reload
on the div surrounding the app like <div data-sveltekit-reload>%sveltekit.body%</div>
but it didn't help.
I did get it to work by not setting base
and instead manually rewriting the paths in the init object, like this:
Promise.all([
import("/wp-content/plugins/sveltekit-wordpress-plugin/sveltekit/build/_app/immutable/entry/start.5d2b6790.js"),
import("/wp-content/plugins/sveltekit-wordpress-plugin/sveltekit/build/_app/immutable/entry/app.cbd67c23.js")
]).then(([kit, app]) => {
kit.start(app, element);
});
You can do this with a regex replace in PHP for example.
It does work, but the routing is funny, eg if you have a /foo
route then when you init the app on wordpress-site.com/foo
then the Svelte /foo
route will load, which is probably not what you want when you're embedding the app. My naive approach would be that you could feed the path to the SvelteKit app when initializing it, as that's easily done from PHP.
I also changed app.html
to be just:
%sveltekit.head%
<div data-sveltekit-reload>%sveltekit.body%</div>
... so that you wouldn't have multiple body tags and such.
I encountered exactly the same problem today. Is there another, perhaps less messy solution?
Describe the bug
Page keeps reloading in WordPress admin page after the all the build files have been referenced properly. It reloads in an infinite loop.
The
+layout.js
is set as,The
svelte.config.js
file is condifgured as,The
vite.config.js
is set as,When the build is run it produces several files in an
_app
folder and aversion.json
file. The files are copied into the WordPress pluginsassets
folder and are referenced using theadmin_head
action hook to do the following,The script within the body of the page is modified as follows,
When the page is loaded, it keeps reloading.
Reproduction
Load the page when all the scripts have been properly referenced in the WordPress plugin
Logs
No response
System Info
Severity
blocking all usage of SvelteKit
Additional Information
No response