sveltekit-wp-bs5 Svelte Themes

Sveltekit Wp Bs5

Experimental, try out SvelteKit with bootstrap5, WordPress, WPGraphQL & more.

February 2023: See my other experiment as well with WordPress REST API, https://github.com/mandrasch/sveltekit-pico-headless-wp

sveltekit-wp-bs5

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.

Local Development Setup

Backend (via DDEV)

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%/'
  • ๐Ÿ‘ทโ€โ™‚๏ธ TODO: https://github.com/matepaiva/wp-graphql-crb needs to be installed as well, right now only possible with composer?
  • ๐Ÿ‘ทโ€โ™‚๏ธ TODO / decide: Use (classic editor) or use Gutenberg (rich, but complex?)? ddev wp plugin --activate classic-editor
  • ๐Ÿ‘ทโ€โ™‚๏ธ TODO / decide: Use atlas-content-modeler (how can we store json for data model in git?) or cpt-ui (types generation can be exported to PHP? ddev wp plugin --activate custom-post-type-ui)

Afterwards you only need to run

ddev start

Frontend (SvelteKit)

cd frontend/
npm install

.env.local already exists, just run

npm run dev

to start local development.

TODO / missing features


Notes (maybe outdated)

Stack / tutorials:

** Frontend **

  • SvelteKit
  • bootstrap5

** Backend / Wordpress: **

Big thanks to

Try it out in Gitpod

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.

Use it locally

See "How can you use this" below.

Technical background

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.

TODOs

How was this created?

  1. Create (empty) github repo without license/README
  2. Clone it to local harddrive, go to folder:
  3. Run npm init svelte@next in the new repo folder
  4. npm install bootstrap - install bootstrap5
  5. Add preprocess npm install --save-dev svelte-preprocess
  6. npm install --save-dev sass postcss autoprefixer for scss
  7. Add preprocess config for postcss autoprefixer & sass in config svelte.config.js
  8. Run npm i --save-dev @sveltejs/adapter-static@next, add to config, see here: https://github.com/sveltejs/kit/tree/master/packages/adapter-static#usage
  9. Added .vscode/-folder,.github/(action)-folder + gitpod.yml config
  10. Add process.env.PATHS_BASE & dev to config
  11. Add src/route/__layout.svelte for scss and js loading
  12. Add gutenberg standard styles npm install @wordpress/block-library --save, add them them to __layout.svelte thanks to https://www.youtube.com/watch?v=Naz0Fv_VVQk
  13. And most importantly: Add static/.nojekyll otherwise GitHub Pages won't work!! ๐Ÿคฆ

Big thanks to

How can you use this?

1. Setup a wordpress (locally or live)

As backend you need to connect a wordpress instance. Use for example DDEV or local for a local wordpress instance.

  1. Install WordPress
  2. Install plugin https://wordpress.org/plugins/wp-graphql/
  3. Add fake data via https://de.wordpress.org/plugins/fakerpress/ e.g. (unfortunately no gutenberg support) or https://artisanthemes.io/sample-content-blocks-gutenberg/ (needs a lot of rework)

2. Develop your SvelteKit-frontend ๐Ÿงก

  1. Clone this repo
  2. Run npm install
  3. Create .env.local (just copy env.local.example) and insert your wordpress URL, e.g. https://mywp-site.xyz/graphql
  4. Run npm run dev

Below are the official SvelteKit instructions for the frontend:

Developing

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

Building

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.

Top categories

Loading Svelte Themes