npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eleventy-plugin-react-ssr

v1.0.3

Published

Pluginto write your Eleventy content using JSX as a template language

Downloads

27

Readme

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.