GithubHelp home page GithubHelp logo

ukabuer / muggle Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 1.13 MB

A server side rendering framework for preact, with islands and PJAX inside.

JavaScript 3.64% TypeScript 94.95% SCSS 1.41%
static-site-generator cms website blog preact islands pjax

muggle's Introduction

Muggle

An unambitious static site generator, no magic inside.

Each page's data and template are defined by yourself, and the route is derived by its file path.

WARNING: this project is still under development.

Features

  • Lightweight and intuitive
  • Write a SSR site like writing a SPA
  • SEO friendly

Install

Make sure you have nodejs >= 14.0.0 and npm installed beforehand, then run

npm install --save muggle

Get Started

1. Static Route

All *.tsx file under directory pages will become a HTML file, as long as it default exports a valid preact component

  • pages/about/index.tsx -> about/index.html
  • pages/404.tsx -> 404.html

Use Head from muggle/client to define something in HTML's <head></head> tag:

import { Head } from "muggle/client";

export default () => {
  return (
    <div>
      <Head>
        <title>My Page Title!</title>
      </Head>
      <h1>Hello</h1>
    </div>
  );
};

2. Load Data

Define a named export preload with a async function to populate component's prop page

export default ({ page }) => {
  return (
    <div>
      <Head>
        <title>My Page Title!</title>
      </Head>
      <h1>{page}</h1>
    </div>
  );
};
// will become <h1>Hello</h1> in HTML

export async function preload(fetch) {
  return "Hello";
}
// you can fetch() something from Internet here

Codes in preload function should be platform independent, it will be excuted at both server side and client side.

3. Dynamic Route

Surround a file or directory's name with bracket to make it a variable:

  • pages/blog/[post].tsx -> blog/first-post.html & blog/second-title.html

You can access the variable's value in the preload function:

// for `pages/blog/[post].tsx`
export async function preload(fetch, params) {
  try {
    const res = await fetch(`https://xxxx.com/blog/${params.post}`);
    return await res.json();
  } catch (err) {
    return { error: "Not Found" };
  }
}

Using Link from muggle/client to make links to the site:

import { Head, Link } from "muggle/client";

export default () => {
  return (
    <div>
      <Head>
        <title>My Page Title!</title>
      </Head>
      <div>
        <p>
          <Link to="/blog/first-post/">First Post</Link>
        </p>
        <p>
          <Link to="/blog/2/">Second Post</Link>
        </p>
      </div>
    </div>
  );
};

4. Server side logic

All *.json.ts files under directory apis will become a JSON file, as long as it named exports a get function

  • apis/blog/list.json.ts -> apis/blog/list.json

You can read data from file system, or fetch something from Internet:

import fs from "fs";
import marked from "marked";

export const get = async (req) => {
  const path = "/home/xxx/blog/markdowns" + req.path;
  // or fetch from Internet
  const markdown = await fs.readFile(path, "utf8");
  return marked(markdown);
};

And you can access it from the preload function:

export async function preload(fetch, params) {
  return fetch(`/apis/blog/${params.post}.json`);
}

export default ({ page }) => {
  return (
    <div>
      <Head>
        <title>{page.title}</title>
      </Head>
      <article dangerouslySetInnerHTML={{ __html: page.content }}></article>
    </div>
  );
};

Devolop or build

Start a dev server, using npm script to run

muggle serve

Generate HTML files:

muggle build

muggle's People

Contributors

dependabot[bot] avatar ukabuer avatar

Watchers

 avatar

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.