Coder Social home page Coder Social logo

kaleidawave / prism-ssr-demo Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 2.0 13 KB

A example of using Prism to build fullstack sites with Express and Node

Home Page: https://kaleidawave.medium.com/isomorphic-sites-hydration-and-prism-compiler-7fc2a24d0c28

CSS 6.08% JavaScript 93.92%
prism prism-compiler isomorphic express spa hydration

prism-ssr-demo's Introduction

Prism SSR Express app

Run on Repl.it

This is a example of using the Prism compiler to build a fullstack application.

To run

# install dependencies
npm install

# compile prism files
npm run build

# run app
npm start

About

After spinning up navigate to localhost and open dev tools:

Running the following in the console:

const feedPage = document.querySelector("feed-page");
JSON.stringify(feedPage.data, 0, 4);

The following gets a instance of feed-page and serializes its data property. You should see a object with a array of posts here that mirrors the markup.

{
    "posts": [
        {
            "title": "post1",
            "id": "post1",
            "description": "Lorem Ipsum",
            "upvotes": 123
        },
        {
            "title": "post2",
            "id": "post2",
            "description": "Lorem Ipsum",
            "upvotes": 3233
        },
        ...
    ]
}

This isn't particular special but what is special is where the this data has come from. Viewing the source there is the server markup rendered but no JS or JSON object sent along with the markup. This is one of the main features of the Prism compiler is that it compiles backwards markup to object hooks. The frontend recreated the posts object in index.js from interpreting the server rendered markup.

Many sites adopting isomorphic rendering often have this problem of continuing on from server state. The quick and dirty way is to render in the server state as JS or JSON object. For example window.__preloadedData on https://www.nytimes.com/. These blobs often contain more information than the page needs and are equivalent to much of the markup.

Using a backwards compiled getter technique should drastically reduce the size of the original payload. It also does not block JS execution or load the objects into memory at startup so it should have better startup times. It also can handle data types so in the serialized state the upvotes field of the post is loaded in as a number.

The getters do have a little bit of overhead but it is fixed invariant overhead which can be cached. Compiling a minified bundle npm run build -- --minify produces a 10kb js bundle (3kb gzip) which puts it in the tiny framework ball park.

The recreation of server state is lazy and paths are only triggered when needed. The state is also partial so the frontend can resolve a property on array without resolving other members of a array (you can see this in action while watching feedPage._data which is the partial state). This is contrary to React and other vdom based frameworks as their hydration requires the whole object to generate the first "vdom frame".

The implementation is a literal proxy so as well as gets sets are also possible:

feedPage.posts[2].description = "Post 3 Description"

and it supports mutation:

feedPage.data.posts.push({
    title: "post8",
    id: "post9",
    description: "Lorem Ipsum",
    upvotes: 8
})

Prism compiled server side templates are simple template literals and can be found (once compiled) in ssr-templates. These templates should be fast as they are in the simplest form. (they are also type safe when built with npm run build -- --backendLanguage ts).

The prism app which is loaded is also a SPA, the "View Post" anchor tags are overwritten to make a spa redirect. The such swaps out the feed-page instance with post-page and retains the header etc. The load cb in only has load the JSON from the server rather than the whole markup and all adornments. Not utilized here but it allows instant navigation with placeholder content for better ux.

While some of the more reactivity elements of prism are still experimental and prone to issues prism as core is a single source is a way to build isomorphic web apps. It takes single template source which is compiled for client side and server side contexts.

For more on prism syntax, design and settings see the main prism repo.

prism-ssr-demo's People

Contributors

kaleidawave avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

icodein shhytim

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.