Giter Club home page Giter Club logo

sveltekit-blog-mdx's Introduction

Rodney Lab sveltekit-blog-mdx Github banner

Rodney Lab logo

SvelteKit MDsvex Blog Starter

Netlify Status

sveltekit-blog-mdx

Open in Visual Studio Code

SvelteKit blog starter to help you get going on your next Svelte blog site. The project creates a Progressive Web App (PWA) out of the box. You just need to customise with your logos etc.

Rodney Lab sveltekit-blog-mdx Github banner

See the Sveltekit Blog Starter blog post on the Rodney Lab site for some explanation of what's inside and how to customise. Please drop questions into a comment at the bottom of that page.

Here's the quick start:

Everything you need to build a Svelte blog site, powered by sveltekit-blog-mdx.

Creating your Own MDsveX Blog Site

If you're seeing this, you've probably already done this step. Congrats!

git clone https://github.com/rodneylab/sveltekit-blog-mdx.git my-new-mdsvex-blog
cd my-new-mdsvex-blog
pnpm install # or npm install
npm run dev

Responsive Images

The starter creates and caches responsive images using the vite-imagetools plugin. This is straightforward to use when you know ahead of time which image you want to include. You just import it on the page you want to use it on:

import featuredImageSrc from '$lib/assets/home/home.jpg';

The vite-imagetools plugin will then generate and hash the image. See examples in src/routes/index.svelte.

The example where you want to have a different featured image for each blog post is a little more complicated, though manageable. In this case, you can run a script (see generate-responsive-image-data.js) to iterate though all blog posts, taking the featured image from the blog post front matter. This script can then output the necessary imports into a generated JavaScript file, one for each blog post (see src/lib/generated directory). Finally you can dynamically import that JavaScript file in the blog template load function.

To run the included script at the command like type:

pnpm run gen:images

This should be done each time you add new blog posts. It also generates a low resolution placeholder, to minimise Content Layout shift during page load.

For the script to find your blog post images, add them under the src/lib/assets/blog folder. In that folder, create a new folder whose name matches the post slug and add the images to the new folder. The name of the file needs to match the name you use is the post frontmatter (for featuredImage, for example).

The script may need some tweaking for your use case. Let me know how it can be improved.

XML Sitemap

  • The XML sitemap gets generated in the server route src/routes/sitemap.xml/+server.js. Tweak this file to adjust output.

Make sure your site's URL is defined in .env as the PUBLIC_SITE_URL variable so the correct URLs are output to the site map.

Progressive Web App (PWA)

The starter mostly generates PWA config automatically, including service worker for offline availability and adding meta to the HTML head section. A PWA needs a manifest file detailing logos (for favicons) in different sizes, compatible with various devices. You will need to generate a set of icons in assets/:

  • apple-touch-icon.png
  • favicon.ico
  • icon.svg
  • icon-192.png
  • icon-512.png

You can use free open source tools to generate and optimise these from an input SVG. That resource includes a shell script you can run to automate generation.

The HTML meta for PWAs is added in the component at src/lib/components/PWA.svelte.

You can customise the manifest (including icon file path) by editing src/lib/config/website.js. The following variables feed into the generated manifest.json file:

  • siteTitle,
  • siteShortTitle,
  • siteUrl,
  • backgroundColor,
  • themeColor.

See article on Progressive Web Apps in SvelteKit for more.

Building

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.

What's inside?

.
├── README.md
├── generate-responsive-image-data.js
├── jsconfig.json
├── netlify.toml
├── package.json
├── src
│   ├── app.html
│   ├── content
│   │   └── blog
│   │       ├── best-medium-format-camera-for-starting-out
│   │       ├── folding-camera
│   │       └── twin-lens-reflex-camera
│   ├── global.d.ts
│   ├── hooks.server.js
│   ├── lib
│   │   ├── assets
│   │   │   ├── blog
│   │   │   └── home
│   │   ├── components
│   │   │   ├── BannerImage.svelte
│   │   │   └── ...
│   │   ├── config
│   │   │   └── website.js
│   │   ├── constants
│   │   │   └── entities.js
│   │   ├── generated
│   │   │   └── posts
│   │   ├── styles
│   │   └── utilities
│   │       ├── blog.js
│   │       ├── file.js
│   │       └── image.js
│   ├── routes
│   │   ├── +error.svelte
│   │   ├── +layout.js
│   │   ├── +layout.svelte
│   │   ├── +page.js
│   │   ├── +page.svelte
│   │   ├── [slug]
│   │   │   ├── +layout.svelte
│   │   │   ├── +page.js
│   │   │   └── +page.svelte
│   │   ├── contact
│   │   │   └── +page.svelte
│   │   ├── manifest.webmanifest
│   │   │   └── +server.js
│   │   └── sitemap.xml
│   │       └── +server.js
│   └── service-worker.js
├── static
│   ├── assets
│   ├── favicon.png
│   ├── icon.svg
│   ├── robots.txt
│   └── sitemap.xml
└── svelte.config.js

src/content

  • The src/content/blog is where we need to put our blog posts. Just clean out the sample content and replace it with your views on the world! There is a separate folder for each post, which allows you to keep images, video and other related media specific to a post better organised. We set the browser path from this folder name, so keep that in mind when naming the folders. Write the actual post in a file called index.md within post's folder. Although the file has an .md extension, you can write Svelte in it.

src

  • hooks.server.js we define Content Security Policy (CSP) and other HTTP security headers in here, effective for server side rendered apps. See post on SvelteKit static site HTTP headers to see how to set up CSP etc for static sites.

src/components

  • src/lib/components these are the components we use in pages.

src/lib

  • src/lib/config/website.js for convenience we define properties for the site here such as the site title, contact email addresses and social media accounts. Some properties feed from environment variables. See a post on getting started with SvelteKit for more on environment variables in SvelteKit.

  • src/lib/styles does what you expect! We use SCSS for styling and source self-hosted fonts in the layouts.

src/utilities

  • src/utilities/blog.js this file contains some code for helping us transform the markdown in blog posts to Svelte. As well as that they help extract fields in the frontmatter (this is the metadata we include at the top of the blog post index.md files).

src/routes

  • src/routes/[slug]/+page.js this is essentially a template for blog post data. One of these files is generated at build for each blog post. It is used to extract data needed in the Svelte file used to generate the post's HTML.

  • src/routes/[slug]/+page.svelte similarly to the previous file, one of these is generated for each blog post. This time it is the Svelte code which SvelteKit uses to generate the HTML and JavaScript for our blog posts.

I mention most of the other files in the Getting Started with SvelteKit blog post, but let me know if I have missed anything which needs more explanation.

Feel free to jump into the Rodney Lab matrix chat room.

sveltekit-blog-mdx's People

Contributors

rodneylab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sveltekit-blog-mdx's Issues

error 404 on back - on mobile android chrome

Describe the bug
Cannot replicate on desktop only on Mobile Android chrome.

To Reproduce
Steps to reproduce the behaviour:

  1. Goto a page and swipe to go back on the phone
  2. Displays error page with the message 404: Not found: /index.html

if I refresh the page the page generates fine.
I tried adding prerender to the index

	export const prerender = true;
</script>

also tried to add _redirects
/* /index.html 200

Expected behaviour
Back on mobile should show previous page and not error

Smartphone (please complete the following information):

  • Device: Android
  • Browser Chrome
  • Version 102

Additional context
Hosted on Netlify can recreate by going here -
https://myairportdiary.co.uk/
Select Subscribe button and then swipe to go back on mobile.

Attached config

/** @type {import('@sveltejs/kit').Config} */
import adapter from '@sveltejs/adapter-static';
import { imagetools } from 'vite-imagetools';
import { mdsvex } from 'mdsvex';
import preprocess from 'svelte-preprocess';

const config = {
  extensions: ['.svelte', '.md', '.svelte.md'],
  preprocess: [
    mdsvex({ extensions: ['.svelte.md', '.md', '.svx'] }),
    preprocess({
      scss: {
        prependData: "@import 'src/lib/styles/variables.scss';",
      },
    }),
  ],
  kit: {
    adapter: adapter({ precompress: true }),
    files: {
      hooks: 'src/hooks',
    },
    prerender: { default: true },
    vite: {
      define: {
        'process.env.VITE_BUILD_TIME': JSON.stringify(new Date().toISOString()),
      },
      plugins: [imagetools({ force: true })],
    },
  },
};

export default config;

[email protected] dev

Describe the bug
404 on accessing blog posts.

To Reproduce
Steps to reproduce the behavior:

  1. Created a clean install of latest commit - [email protected] dev
  2. Selected a blog post ie best-medium-format-camera-for-starting-out
  3. Console returns error
failed to load module for ssr: /work/tmp/content/blog/best-medium-format-camera-for-starting-out/index.md
Error: failed to load module for ssr: /work/tmp/content/blog/best-medium-format-camera-for-starting-out/index.md
    at instantiateModule (C:\work\tmp\my-new-mdsvex-blog\node_modules\vite\dist\node\chunks\dep-80fe9c6b.js:50206:15)
  1. Browser returns 404

Expected behaviour
Blog post display

Screenshots
N/A

Desktop (please complete the following information):

win/chrome canary
node v16.14.0

Smartphone (please complete the following information):

n/a

Additional context
Add any other context about the problem here.

Support for Internationalization???

hello, I want to thank you for this project. this is awesome and may I ask if there a possible way to add internationalization to blog post ???

one way I can think of is make a different mdx file with localized tag and generate it from there but I want to ask your opinion first in this matter

Error: request in handle has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details

Describe the bug
Running in VS Code dev containers, I'm getting the following error:

request in handle has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details
Error: request in handle has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details
    at Object.get request [as request] (file:///workspaces/sveltekit-blog-mdx/node_modules/@sveltejs/kit/dist/chunks/index.js:1678:11)
    at Object.handle (//workspaces/sveltekit-blog-mdx/src/hooks:69:32)
    at respond (file:///workspaces/sveltekit-blog-mdx/node_modules/@sveltejs/kit/dist/chunks/index.js:1585:30)
    at file:///workspaces/sveltekit-blog-mdx/node_modules/@sveltejs/kit/dist/chunks/index.js:1910:30

To Reproduce
Steps to reproduce the behaviour:

  1. Go to open.vscode.dev/rodneylab/sveltekit-blog-mdx
  2. Run through various setup options, which I cant recall now
  3. Run npm install
  4. Run pm run dev

Expected behaviour

Expect the web project would be build and run without errors

Additional context

Error output

request in handle has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details
Error: request in handle has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details
    at Object.get request [as request] (file:///workspaces/sveltekit-blog-mdx/node_modules/@sveltejs/kit/dist/chunks/index.js:1678:11)
    at Object.handle (//workspaces/sveltekit-blog-mdx/src/hooks:69:32)
    at respond (file:///workspaces/sveltekit-blog-mdx/node_modules/@sveltejs/kit/dist/chunks/index.js:1585:30)
    at file:///workspaces/sveltekit-blog-mdx/node_modules/@sveltejs/kit/dist/chunks/index.js:1910:30

Having trouble building `npm run build`

Describe the bug
Hey I'm not able to build the repo properly.

To Reproduce
These are the steps I took:

  1. git clone into a new folder
  2. run npm i => got error that my node version needed to be upgraded
  3. run brew install node (node version now v17.3.0)
  4. run npm i (worked)
  5. run npm run dev (worked)
  6. run npm run preview (worked)
  7. run npm run build

Expected behaviour
Just want the repo to build with all the styles and JS.

Screenshots
image
image

Looks like

Desktop (please complete the following information):

  • OS: Mac 11.2.3
  • Browser n/a
  • Version just cloned it

Additional context
Add any other context about the problem here.

Build error (vercel)

Describe the bug
Vercel gives an error while building this project!

To Reproduce

  1. Connect Vercel with this repo
  2. Start building now
  3. The error will show up

Expected behaviour
A successful build

Additional context

Here's the build log:

TypeError: Cannot read property 'content' of undefined
12:24:08.959 | at get (file:///vercel/path0/.svelte-kit/output/server/entries/endpoints/_slug_/index.json.js:11:38)
12:24:08.959 | at render_endpoint (file:///vercel/path0/.svelte-kit/output/server/app.js:150:26)
12:24:08.959 | at resolve (file:///vercel/path0/.svelte-kit/output/server/app.js:1819:59)
12:24:08.959 | at async Object.handle (file:///vercel/path0/.svelte-kit/output/server/app.js:1935:20)
12:24:08.960 | at async respond (file:///vercel/path0/.svelte-kit/output/server/app.js:1770:22)
12:24:08.960 | at async fetch (file:///vercel/path0/.svelte-kit/output/server/app.js:1206:22)
12:24:08.960 | at async load (file:///vercel/path0/.svelte-kit/output/server/entries/pages/_slug_/index.svelte.js:150:15)
12:24:08.961 | at async load_node (file:///vercel/path0/.svelte-kit/output/server/app.js:1286:14)
12:24:08.961 | at async render_page (file:///vercel/path0/.svelte-kit/output/server/app.js:1651:20)
12:24:08.964 | TypeError: Cannot destructure property 'datePublished' of 'post' as it is undefined.
12:24:08.964 | at file:///vercel/path0/.svelte-kit/output/server/entries/pages/_slug_/index.svelte.js:80:11
12:24:08.964 | at Object.$render (file:///vercel/path0/.svelte-kit/output/server/chunks/index-13c0de55.js:90:18)
12:24:08.964 | at file:///vercel/path0/.svelte-kit/output/server/entries/pages/_slug_/index.svelte.js:172:54
12:24:08.964 | at Object.$render (file:///vercel/path0/.svelte-kit/output/server/chunks/index-13c0de55.js:90:18)
12:24:08.965 | at Object.default (file:///vercel/path0/.svelte-kit/output/server/app.js:61:96)
12:24:08.965 | at file:///vercel/path0/.svelte-kit/output/server/entries/pages/_slug_/__layout.svelte.js:4:42
12:24:08.965 | at Object.$render (file:///vercel/path0/.svelte-kit/output/server/chunks/index-13c0de55.js:90:18)
12:24:08.965 | at Object.default (file:///vercel/path0/.svelte-kit/output/server/app.js:55:111)
12:24:08.965 | at file:///vercel/path0/.svelte-kit/output/server/entries/pages/__layout.svelte.js:176:82
12:24:08.965 | at Object.$render (file:///vercel/path0/.svelte-kit/output/server/chunks/index-13c0de55.js:90:18)
12:24:08.967 | > 500 /undefined (linked from /)
12:24:08.968 | at file:///vercel/path0/node_modules/@sveltejs/kit/dist/chunks/index5.js:408:11
12:24:08.968 | at visit (file:///vercel/path0/node_modules/@sveltejs/kit/dist/chunks/index5.js:585:4)
12:24:08.968 | at processTicksAndRejections (internal/process/task_queues.js:95:5)
12:24:09.009 | error Command failed with exit code 1.
12:24:09.009

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.