I’ve decided try out Svelte for my next website, and this will be a static website hosted using GitLab pages.
I got the basic compilation working which generates dist/bundle.js
and dist/bundle.css
.
The issue is that I cannot upload this dist
folder as there is no index.html
file.
How do I get Svelte/rollup to generate an index.html
file which contains the initial render?
The other option is to create my own index.html
file and import bundle.js
. This is not an option for me because the initial render is now generated at runtime via javascript instead of at compile-time, potentially having a negative SEO impact and preventing users without javascript from at least seeing something.
I was also looking at Sapper which does server-side rendering, which, from what I know, does an initial rendering server-side. However, this seems to require you to have a server instead of rendering to a file, and seems overly complicated for a static single-page website.
2
Answers
After digging around for a while, I found out that Sapper allows you to export (render to html files instead of requiring a server).
You can do this by using the
sapper export
command. You can also easily switch to an express server if required.This has the following benefits over a normal svelte compilation and over some other frameworks:
I have recently started experimenting with Svelte and started by downloading the hello world example.
I then just started altering it for my needs.
It already has an
index.html
file set up in the public folder (it is set up to compile to the public folder instead of dist). Svelte / Rollup will not generate anindex.html
file, it is purely for compiling and bundling your JS / Svelte components.The
index.html
file supplied is just basic:The
main.js
looks like this:Here is a link [source], [build] to my first svelte app if you’re interested.
As far as SEO is concerned, I hear all over the place for years that google can crawl JS now, but I am not convinced. A JS driven SPA will never have the SEO juice that a standard html page will.
That being said, I am currently working on an SPA with svelte that I want good SEO for. The interactive part is only a small part of the page, so I am adding the rest (text, images and stuff) directly to the
index.html
so search engines should have no problem crawling it. I just change themain.js
to inject the app into a div (with the ID of app) rather than the body.So the
main.js
looks like this:I have not yet done anything with Sapper so I can’t comment on that.
I hope my answer helps in some way.