This is starter template to create multiple-page application using plain Svelte, so just like SvelteKit static-adapter, but without unecessary complexity it introduces.
The purpose of this project is to make Svelte that defaults is component-based SPA into an statically-generated MPA (multi-page SPA, page+component-based).
So for deployment you only need to rsync the .html
, imported/external .css
/.js
and any other assets (images, fonts, non-.svelte
files).
It was originally built for Z template engine.
Creator: sameerveda
Specs/maintainer/sponsored by: kokizzu
Rethinking Reactivity
tl;dw
.svelte
file is will became html element2023-08-07
feat: remove full path fakecss comment generation2023-08-02
feat: send desktop notification when error2023-08-02
bugfix: should not exit when compile error2023-06-23
feat: upgrade to svelte-42023-01-07
bugfix: nested component css not outputted after editing2022-12-31
bugfix: component css not outputted after editing2022-09-12
feat: render on first load for SEO2022-07-07
bugfix: watcher not capturing added/deleted files, feat: optimized watch list2022-05-30
initial versionFor example you have a project with specific structure:
_mycomponent/
button.svelte
foo/
_table.svelte
bar.svelte --> will generate bar.html
any.js
subpage/
page3.svelte --> will generate page3.html
index.svelte --> will generate index.html
whatever.css
whatever.js
_layout.html
It would automatically generate 3 files: foo/bar.html
, subpage/page3.html
, and index.html
.
.html
foreach .svelte
, for production build use npm run build:prod
.html
, eg. foo/_component1.svelte
, or _components/table.svelte
_layout.html
in current directory or nearest upper directories as base templatenpm run dev
, it would listen to localhost:5500
then livereload when changed like default svelte template project (will also autogenerate the .html
files like spec number 1), there's also npm run watch
if you have reverse proxy or backend that loads html that listen on another port./update_deps.sh
to update dependencies)/*! c1 */
, #{c2}
, [/* c3 */]
, or {/* c4 */}
Svelte-MPA is just consist of build.js
, package.json
, svelte.config.js
, so you just need to copy those files to your project, then create _layout.html
and one .svelte
file to start. Or alternatively just clone whole directory manually or using degit
and remove stuff that you don't need.
npm install -g degit # scaffolding helper
degit kokizzu/svelte-mpa myproject1 # clone this repo with new name
cd myproject1
npm install # install dependencies
npm start # start dev-server, auto rebuild
npm run watch # auto rebuild without webserver,
# eg. if you use other webserver/reverse proxy locally
npm run build:prod # build project for production
./deploy.sh # example deployment script for single server
just copy latest build.js
to your project directory and then npm i
or pnpm i
or if you want to use Svelte-4 from previous version that uses Svelte-3:
# copy latest build.js to your project directory
cp ../svelte-mpa/build.js .
cp ../svelte-mpa/svelte.config.js .
# upgrade package.json to svelte 4
npx svelte-migrate svelte-4 .
# update all dependencies (downloads svelte 4)
./update_deps.sh # or npm i # or pnpm i
š„ ZERO production dependency. š„
If you already have existing backend that wasn't written in NodeJS, you can have best of two worlds, multiple page with their own meta headers and content for SEO, and reactivity using Svelte for each page. Also with this you can remove the serialization/transport/hop-cost of default setup:
so for first request normally would be something like this:
[Browser]
--fetch-HTML-->
[SvelteKit/Next/Nuxt/other-SSR]
--fetch-API-->
[ExistingBackend]
or like this:
[Browser] (1st request)
--fetch-HTML-->
[SvelteKit-static-adapter/generated-HTML] (fetch the HTML first without data)
[Browser] (2nd request)
--fetch-API-->
[ExistingBackend] (fetch the JSON to populate data, then rerender)
became:
[Browser] (only need 1 request)
--fetch-HTML+API-->
[ExistingBackend]
So your existing backend responsibility is to load the generated .html
then replace the js variable or any template keyword with proper value for initial load/SEO. So not svelte's responsibility to request/preload the initial json content, but backend's responsibility (whatever existing backend langauge/framework you are using). Like SvelteKit, you can also use this as SSG. You can see example here
<!-- MODIFIED TIME
generated comment from highest modification date of dependencies, eg. if a.svelte
depends on b.js
and _c.svelte
, the resulting a.html
html comment should be max modification date of those three[name].min.js
foreach [name].svelte
file that will imported by generated [name].html
, to reduce overhead when page's bundled reactivity code size is very big, only when svelte file doesn't contain Z-template special keywords, so the code might look like this:
```html
_layout.html
:
...bla.svelte
:
generated bla.min.js
will be referenced by bla.html
: