GithubHelp home page GithubHelp logo

eleventy-plugin-react-ssr's Introduction

eleventy-plugin-react-ssr

This plugin allows you to write your Eleventy content using JSX as a template language, and render it using React server-side render.

Installation

yarn add --dev eleventy-plugin-jsx

Usage

Enabling the plugin

The first step is to enable the plugin in your Eleventy config

// .eleventy.js
const eleventyReactSSRPlugin = require('eleventy-plugin-react-ssr');

module.exports = function (eleventyConfig) {
    eleventyConfig.addPlugin(eleventyReactSSRPlugin);
};

Plugin configuration

This plugin uses Babel to transpile JSX files. It is possible to pass any Bable config as plugin configuration using the property babelConfig. For example, you can provide your own Babel plugins:

const eleventyReactSSRPlugin = require('eleventy-plugin-react-ssr');

module.exports = function (eleventyConfig) {
    eleventyConfig.addPlugin(eleventyReactSSRPlugin, {
        babelConfig: {
            plugins: [['inline-react-svg', { svgo: false }]],
        },
    });
};

Writing pages

Just use the extension .jsx for your page, and write it using JSX.

Pages are expected to have a default export with the main component (either Function Component or Class Component, both will work):

function MyPage() {
    return <h1>Hello world</h1>;
}

export default MyPage;

Defining page data

If you need to define page data (similar to a front matter in a Markdown page), use the static property data

function MyPage() {
    return <h1>Hello world</h1>;
}

MyPage.data = {
    title: 'Hello world',
    layout: 'main',
    permalink: 'hello.html',
    customData: {
        foo: 'bar',
    },
};

export default MyPage;

Using page data

You may want to use the data provided by Eleventy in your page. This can be done using React Context and a hook:

import EleventyContext from 'eleventy-plugin-react-ssr/context';
import { useContext } from 'react';

function MyPage() {
    // Access to anything from the data cascade, including page data
    // and Eleventy supplied data from https://www.11ty.dev/docs/data-eleventy-supplied/
    const { title, customData, page } = useContext(EleventyContext);

    return (
        <>
            <h1 className={customData.foo}>{title}</h1>
            <p>URL: {page.ur}</p>
        </>
    );
}

MyPage.data = {
    title: 'Hello world',
    customData: {
        foo: 'bar',
    },
};

export default MyPage;

Writing layouts

It is possible to write layouts using JSX too. However, I'll recommend to only use layouts if the content comes from a diffrent template language (eg: you have content in Markdown that you'd like to render inside a JSX layout). If you want to do "JSX in JSX" (content in JSX, layout in JSX as well), check the next section.

// page.md
---
layout: layout/main.jsx
title: My page
---
My page is aweseome
// _includes/layout/main.jsx
import EleventyContext from 'eleventy-plugin-react-ssr/context';
import { useContext } from 'react';

function MainLayout() {
    const { content, title } = useContext(EleventyContext);

    return (
        <>
            <h1>{title}</h1>
            <div dangerouslySetInnerHTML={{ __html: content }} />
        </>
    );
}

export default MainLayout;

Page components

If you don't have content in other template languages (ie: all your pages are JSX), then I'd recommend to not use layouts, and use components instead.

// ./_includes/components/html-page.jsx

export function HTMLPage({ lang, children }) {
    return (
        <html lang={lang}>
            <head>{/* Link to styles, scripts, etc. */}</head>
            <body>{children}</body>
        </html>
    );
}
// page.jsx
import { HTMLPage } from './_includes/components/html-page';

function MyPage() {
    return (
        <HTMLPage lang="en">
            <h1>Hello world</h1>
        </HTMLPage>
    );
}

export default MyPage;

This way, the page will be JSX all the way to the end, and you can pass props to the page component.

If you really want to, you can use a JSX layout for a JSX page. It will work, but it will be a bit hacky as you need to use dangerouslySetInnerHTML to render it, and use page data to simulate passing props.

eleventy-plugin-react-ssr's People

Contributors

scinos 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.