February 2023: See my other experiment as well with WordPress REST API, https://github.com/mandrasch/sveltekit-pico-headless-wp
Beware: This is not yet SvelteKit 1.0, needs updates/migration. https://github.com/sveltejs/kit/discussions/5774
Experimental learning project, connecting a SvelteKit-frontend (made with bootstrap5) to a WordPress backend via WPGraphQL-plugin. This is called a 'headless WordPress'-setup. It's work in progress.
New goal: Use only open source plugins
Status: ๐ง Work in progress, currently conversion to monorepo and experiment with carbon fields usage ๐ง
Live-Demo: mandrasch.github.io/sveltekit-wp-bs5/
WordPress : WP-Backend (can later be hidden completely)
๐บ Teaser-Video: https://www.youtube.com/watch?v=gqRaN8Bojv0 (ACF might be replaced with carbon fields or similiar)
First time install (see WordPress quickstart):
cd wp-backend/
ddev start && ddev exec wp core download && ddev launch
After installation, setup our child theme and graphql plugins (via WPCLI):
ddev wp theme activate twentytwentytwo-child
ddev wp plugin install --activate wp-graphql atlas-content-modeler
ddev wp plugin install --activate https://carbonfields.net/zip/latest/
ddev wp rewrite structure '/%postname%/'
ddev wp plugin --activate classic-editorddev wp plugin --activate custom-post-type-ui)Afterwards you only need to run
ddev start
cd frontend/
npm install
.env.local already exists, just run
npm run dev
to start local development.
** Frontend **
** Backend / Wordpress: **
Big thanks to
Note for Gitpod: You must create .env.local (see .env.local.example) in order to connect a WordPress instance with WPGraphQL-plugin. Use CTRL + c to abort and run npm run dev afterwards again.
See "How can you use this" below.
Currently I use @sveltejs/adapter-static, but I also want to test (sustainable) node hosting with @sveltejs/adapter-node on providers like render.com in future.
@sveltejs/adapter-static - static generation, only HTML/JS/CSS is delivered to the user. If WordPress content changes, a Webhook must be called (via https://de.wordpress.org/plugins/wp-webhooks/ for example) to trigger a new build@sveltejs/adapter-node, e.g. via nodeJS hosting - not explored this yet, for sustainability there is also prerendering / static generation involved (See: https://twitter.com/KellenMace/status/1469044771729690624)npm init svelte@next in the new repo foldernpm install bootstrap - install bootstrap5npm install --save-dev svelte-preprocessnpm install --save-dev sass postcss autoprefixer for scsssvelte.config.jsnpm i --save-dev @sveltejs/adapter-static@next, add to config, see here: https://github.com/sveltejs/kit/tree/master/packages/adapter-static#usage.vscode/-folder,.github/(action)-folder + gitpod.yml configsrc/route/__layout.svelte for scss and js loadingnpm install @wordpress/block-library --save, add them them to __layout.svelte thanks to https://www.youtube.com/watch?v=Naz0Fv_VVQkstatic/.nojekyll otherwise GitHub Pages won't work!! ๐คฆBig thanks to
As backend you need to connect a wordpress instance. Use for example DDEV or local for a local wordpress instance.
npm install.env.local (just copy env.local.example) and insert your wordpress URL, e.g. https://mywp-site.xyz/graphqlnpm run devBelow are the official SvelteKit instructions for the frontend:
Once you've created a project and installed dependencies with npm install (or pnpm install or yarn), start a development server:
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
Before creating a production version of your app, install an adapter for your target environment. Then:
npm run build
You can preview the built app with
npm run preview, regardless of whether you installed an adapter. This should not be used to serve your app in production.