Skip to content

✨ SPA with Vue 3 and Kirby: SEO-friendly, automatic routing, mulit-lang-ready and more!

License

Notifications You must be signed in to change notification settings

cideight/kirby-vue3-starterkit

 
 

Repository files navigation

Logo of Kirby + Vue.js 3 Starterkit

Kirby + Vue Starterkit

SPA with Vue 3 and Kirby: SEO-friendly, automatic routing, mulit-lang-ready and more!
Explore the starterkit live »


Kirby + Vue 3 Starterkit

Key Features

Introduction

This project is a starting point for Vue.js v3 as the frontend UI library and Kirby as headless CMS. The content is provided as JSON through Kirby and fetched by the frontend.

It's a simple, zero-setup, almost identical port of the Kirby 3 starterkit frontend (snippets, templates and their corresponding JS/CSS) to Vue.js single file components. By "almost" meaning that some features have been added like meta tags generation, environment variables support, accessible routing etc.

To compile the frontend sources, Vite comes to use. Vite is an opinionated web development build tool, created by Evan You. It serves code via native ES Module imports during development, allowing you to develop Vue.js single file components without a bundle step, and bundles it with Rollup for production. Features include:

Lighthouse Report

Lighthouse Report

Folder Structure

Some notes about the folder structure with some additional comments on important files.

Expand folder tree
kirby-vue3-starterkit/
|
|   # Includes all frontend-related files
├── frontend/
|   |
|   |   # Vue.js sources
|   ├── src/
|   |   |
|   |   |   # `Header`, `Footer`, `Intro` and other components
|   |   |   # (Vue.js components correspond to Kirby snippets)
|   |   ├── components/
|   |   |
|   |   |   # Commonly used helpers
|   |   ├── helpers/
|   |   |
|   |   |   # Hooks for common actions
|   |   ├── hooks/
|   |   |   |
|   |   |   |   # Hook to announce any useful information for screen readers
|   |   |   ├── useAnnouncer.js
|   |   |   |
|   |   |   |   # Hook providing information about the current language
|   |   |   ├── useLanguages.js
|   |   |   |
|   |   |   |   # Hook for retrieving pages from the content API
|   |   |   ├── useKirbyApi.js
|   |   |   |
|   |   |   |   # Hook returning the page for current path, similarly to Kirby's `$page` object
|   |   |   ├── usePage.js
|   |   |   |
|   |   |   |   # Hook for various service worker methods like registering
|   |   |   ├── useServiceWorker.js
|   |   |   |
|   |   |   |   # Hook returning a object corresponding to Kirby's global `$site`
|   |   |   └── useSite.js
|   |   |
|   |   |   # Custom Vue plugins
|   |   ├── plugins/
|   |   |   |
|   |   |   |   # Adds a `v-kirbytext` directive to handle internal page links inside KirbyText
|   |   |   └── KirbyTextDirective.js
|   |   |
|   |   |   # Vue Router related methods and exports
|   |   ├── router/
|   |   |   |
|   |   |   |   # Initializes and exports the router instance
|   |   |   ├── index.js
|   |   |   |
|   |   |   |   # Handles the router's scroll behaviour
|   |   |   └── scrollBehaviour.js
|   |   |
|   |   |   # Vue.js views corresponding to Kirby templates
|   |   |   # Routes are being automatically resolved
|   |   ├── views/
|   |   |
|   |   ├── App.vue
|   |   ├── main.css
|   |   ├── main.js
|   |   └── serviceWorker.js
|   |
|   |   # Index page used by Vite in development environment
|   └── index.html
|
|   # Main entry point of the website, point your web server to this directory
├── public/
|   |
|   |   # JavaScript and CSS assets generated by Vite (not tracked by Git)
|   ├── assets/
|   |
|   |   # Static images like PWA icons
|   ├── img/
|   |
|   |   # Kirby's media folder for thumbnails and more (content not tracked by Git)
|   └── media/
|
|   # Various development-centric Node scripts
├── scripts/
|   |
|   |   # Service worker generator
|   └── buildServiceWorker.js
|
|   # Kirby's core folder containing templates, blueprints, snippets etc. for Kirby
├── site/
|   ├── blueprints/
|   ├── config/
|   |   |
|   |   |   # General Kirby configuration settings and configuration settings for plugins
|   |   ├── config.php
|   |   |
|   |   |   # Builds a JSON-encoded `site` object for the frontend
|   |   |   # Used by Vue Router to populate routes, but can be extended by commonly used data
|   |   └── spa-site.php
|   |
|   |   # Only used in multi-language setups
|   ├── languages/
|   ├── models/
|   ├── plugins/spa-adapter/
|   |   |
|   |   |   # Core of the SPA integration plugin, mainly registeres routes
|   |   ├── index.php
|   |   |
|   |   |   # Routes to handle `.json` requests and serving the `index.php` snippet
|   |   └── routes.php
|   |
|   ├── snippets/
|   |   |
|   |   |   # Index page used in production environment – almost identical to `frontend/index.html`
|   |   |   # Handles build asset paths, inlines the `site` object, includes SEO meta tags, etc.
|   |   └── spa-index.php
|   |
|   |   # Templates for JSON content representations fetched by frontend
|   └── templates/
|
|   # Contains everything content and user data related (contents not tracked by Git)
├── storage/
|   ├── accounts/
|   ├── cache/
|   ├── content/
|   ├── logs/
|   └── sessions/
|
|   # Kirby CMS and other PHP dependencies (handled by Composer)
├── vendor/
|
|   # Environment variables for both Kirby and Vite (to be duplicated as `.env`)
├── .env.example
|
|   # Handles PHP dependencies
├── composer.json
|
|   # Handles NPM dependencies
├── package.json
|
|   # Router for the PHP built-in development server (used by `serveKirby.js`)
├── server.php
|
|   # Configuration file for Vite
└── vite.config.js

Caching & Offline Capability With Service Worker

Even without a service worker installed, the frontend will store pages between indiviual routes/views. When you reload the tab, the data for each page is freshly fetched from the API once again.

For offline capability of your Vue app, you can choose to activate the included service worker.

A visual explanation of both methods can be found in the following flow chart:

Caching for Kirby and Vue 3 starterkit

The service worker precaches all CSS & JS assets required by the Vue app and caches the data of every requested page. All assets are versioned and served from the service worker cache directly.

Each JSON request will be freshly fetched from the network and saved to the cache. If the user's navigator turns out to be offline, the cached page request will be returned.

Stale-While-Revalidate

The stale-while-revalidate mechanism for the usePage hook allows you to respond as quickly as possible with cached page data if available, falling back to the network request if it's not cached. The network request is then used to update the cached page data – which directly affects the view after lazily assigning changes (if any), thanks to Vue's reactivity.

Prerequisites

  • Node.js with npm (only required to build the frontend)
  • PHP 7.4+

Kirby is not a free software. You can try it for free on your local machine but in order to run Kirby on a public server you must purchase a valid license.

Setup

  1. Duplicate the .env.example as .env and optionally adapt its values:
cp .env.example .env
  1. Install npm dependencies:
npm install

Note: Composer dependencies are tracked in this repository by default. Running composer install isn't necessary.

Usage

Serve Backend & Frontend for Development

# Runs `npm run kirby:serve` and `vite` in parallel
npm run dev

Out of the box the backend is automatically served while developing. npm run kirby:serve spawns the PHP built-in web server by Node. You can also serve the backend by a web server of your choice. If done so, please specify hostname and port in your .env if they differ from localhostand 8080 respectively so that the decoupled frontend can call the Kirby API for JSON content in development.

Compile for Production

Build the frontend assets (CSS & JS files) to public/assets:

npm run build

Deployment

  1. Deploy the repository on your server.
  2. Duplicate .env.example as .env.
  3. Install npm dependencies and build frontend assets: npm i && npm run build.
  4. Change variables in your .env:
    • KIRBY_CACHE to true (recommended)
  5. Point your web server to the public folder.
  6. Some hosting environments require to uncomment RewriteBase / in .htaccess to make site links work.

Now your project is hopefully up 'n' running!

Configuration

All development and production related configurations for both backend and frontend code are located in your .env file:

  • KIRBY_SERVER_HOSTNAME and KIRBY_SERVER_PORT specify the address where you wish the Kirby backend to be served from. It is used by the frontend to fetch content data as JSON.
  • Keys starting with VITE_ are available in your code following the import.meta.env.VITE_CUSTOM_VARIABLE syntax.

For example setting KIRBY_CACHE to true is useful in development environment.

Content API Slug

To change the API slug to fetch JSON-encoded page data from, set

  • CONTENT_API_SLUG to a value of your liking (defaults to spa). It can even be left empty to omit a slug altogether!

You can't use Kirby's internal API slug (defaults to api). If you insist on using api for your content endpoint, you can rename Kirby's by adding a KIRBY_API_SLUG key and set it to something other than api.

Multi-Language

Multiple languages are supported. A comprehensive introduction about multi-language setups may be found on the Kirby website.

To enable language handling, you don't have to edit the config.php manually. Just set

  • KIRBY_MULTILANG to true.
  • KIRBY_MULTILANG_DETECT to true (optional but recommended).

Then, visit the panel and add new languages by your liking. The Panel automatically renames all existing content and file meta data files and includes the language extension.

Language data is provided by the global site object, which can be accessed via the useSite() hook.

ℹ️ Current limitations:

  • Custom language paths aren't supported as of right now, the language code defined will be used as a base in the frontend.
  • Automatic language detection only works in production environment. In development the fallback language is always the default language.

Service Worker

To enable the service worker which precaches essential assets and page API calls for offline capability, set:

  • VITE_ENABLE_SW to true

⚠️ Don't change the CONTENT_API_SLUG once you deployed your app publicly and thus a service worker is installed on clients. Otherwise fetch requests will fail and a blank page will show until the new service worker is activated, which then is only possible by closing the tab/PWA.

Stale-While-Revalidate

To keep page data fresh with stale-while-revalidate, set:

  • VITE_ENABLE_SWR to true

Credits

License

MIT

It is discouraged to use this starterkit in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

About

✨ SPA with Vue 3 and Kirby: SEO-friendly, automatic routing, mulit-lang-ready and more!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 44.9%
  • JavaScript 33.0%
  • Vue 19.9%
  • CSS 1.4%
  • Other 0.8%