The page you’re looking at is powered by Svelte. The previous version of the site was based on Gatsby, and even earlier on Hexo. When I came across Svelte, I was amazed by its features. So i decided to rewrite my site and take the opportunity to dive into Svelte and its ecosystem.
It took me five weekends to finish the rewriting process. Now, I’d like to share what I did.
SvelteKit is a framework for building web applications. SvelteKit for Svelte is similar to Next.js for React, Nuxt.js for Vue. it offer build tools, filesystem-based routing and rendering practices for us to setup Svelte app quickly.
For development, SvelteKit integrates Vite, which makes both development and production builds extremely fast.
Additionally, I added config to make vite resolve my posts files from project root folder:
The index@page.svelte
files in blog
, draw
, note
and slide
folders serve as index pages for all my posts, drawings, notes and slides.
These pages can have a load function that fetches data for my blog posts before the component is created, and it can even run on the server side with prerender opton:
For the post detail pages, it is necessary to use a template to display different pages for each post. Therefore, I utilized dynamic routing with the index@post-page.svelte
file wrapped in [slug]
folder. The load
function in these page only needs to fetch post data based on the slug
:
SvelteKit supports filesystem-based routing, allowing us to setup layouts, including nested and named layouts.
I added the following 4 layout files:
__layout.svelte
serves as the root layout for my website, defining the main structure of the pages:
The other three files are named layouts, inherited directly from the __layout.svelte
.
__layout-draw-page@default.svelte
and __layout-post-page@default.svelte
are layouts for blog detail pages.
__layout-page@default.svelte
layouts the other pages.
You can see i used names page
, draw-page
and post-page
for different layouts, which correspond to different page files such as index@post-page.svelte
and about@page.svelte
.
I prefer styling with Less and PostCSS. I like the feeling of writing original CSS Code rather than Atomic or Utility-first CSS like Tailwind CSS.
As svelte-preprocess can automatically transforms the code to provide support for PostCSS and Less, I configure the svelte.config.js
:
To render Markdown files, i used mdsvex, which converts Markdown to HTML, supports using Markdown files as component, and allows components to be used within Markdown files via .svx
file.
You can see i added uses.svx
, combining svelte components, html and markdown syntax:
For normal markdown files, I configured mdsvex
into svelte-preprocess in svelte.config.js
:
rehype-slug
plugin can add id
attributes to headings, rehype-autolink-headings
plugin add links from headings back to themselves. They help me to implement hash anchor for headings.
I customized rehype-urls
plugin to fill the empty alt
attribute for img
tag, the target
and rel
attributes for outside a
tag, and specified all the images should be in webp
format:
I used an endpoint, resolving all my posts and serving a XML file, to implement RSS feed.
SSG means Static Site Generation, it is ideal for pre-rendering every page on my website.
SvelteKit provides several adapters, and I chose the static adapter for this purpose:
To make a better performance for SEO, it is important to has an auto generated sitemap.
After using the static-adapter above, i can only generate sitemap files after the whole site was built.
I added a sitemap
script in package.json
:
And a gen-sitemap.js
to read all build files and trimming files url to build sitemap:
That’s what I did with Svelte and SvelteKit for this new website. Hope it can give you some ideas.
Thank you for reading.