Skip to content

pvcresin/pdom

 
 

Repository files navigation


Parallel DOM npm version

Make your apps faster, parallelize away heavy DOM operations.


Usage

$ npm i parallel-dom

Javascript

Example | Demo

import PDom from 'parallel-dom';

const pdom = new PDom(
    // The root, the subtree will be 
    // made parallel.
    '#root', 

    // Javascript entry point of the script 
    // to run parallely inside the parallel subtree.
    () => import('path/to/script/which/runs/in/parallel') 
);
pdom.render();

React

Example | Demo

// parallel-component.tsx
const ParallelComponent = ({ prop1, onCallback }) => {
    // heavy operations.
    // ...
    return <button onClick={onCallback}>{prop1}</button>
}
import PDom from 'parallel-dom/react';

const ParallelComponent = PDom(() => import('./parallel-component'));

export const App = () => {
    const [p1, setP1] = useState('');
    return <>
        ...
        <ParallelComponent prop1={p1} onCallback={cb} />
    </>
}

React limitations

  1. Not supported: unserializable props (JSX in props etc.)
  2. Callbacks in Props are supported, but they are all async.

Under the hood

  1. Creates a dummy shell iframe on a subdomain.
  2. Passes the scriptUrl and creates a clone of the passed DOM element to the above frame.
  3. The iframe loads and runs the script.
  4. Since, iframe is cross origin, the browser creates a dedicated process for it.
  5. We use origin-agent-cluster header to enable multiple PDom to have their own dedicated subframe process.

For more details:

Supported Browsers

  • Chrome
  • Edge

Degrades to single threaded, on unsupported browsers.

Self hosted

You can host PDom on your own if you want to avoid using the pdom.dev domain for some reason.

Vercel

Quick deploy on Vercel.

Deploy with Vercel

Your own infrastructure

  1. git clone https://github.com/pdomdev/pdom
  2. cd pdom
  3. npm i
  4. npm run build
  5. Serve the dist folder with your own static server (like nginx). Or could use http-server.
  6. You could also do npm start to run a local developement server.

Domain and header configuration

  • Configure the web server to add the following response header (Already done in the above template for vercel):
Origin-Agent-Cluster: ?1
  • Configure a wildcard subdomain, with your DNS. Here is some documentation on how to do this with common Domain providers.

FAQ

  • What are you talking about ? iFrames suck!

iFrames were once insecure and often used for nefarious purposes. Browser makers have introduced many new security requirements as either defaults or even removed the older insecure ways entirely. The technology has advanced significantly. Some of the security features PDom uses to secure the iframes:

Sandboxing:

Sanboxed Iframes create a secure context 
with no access to the parent's context.
In browser script injection: 

Your javascript is never hosted on PDom server. 
Its injected at runtime by your parent application to the frame.

  • I don't trust a third party domain. Can I host this myself ?

Absolutely! The whole thing is open source, and we have included documentation on how to host this yourself. We have also built a Vercel template for you to quickly deploy the service if you use Vercel.

FWIW, you could use any static hosting provider, like github pages, netlify etc. As PDom does not really need a backend server, it's purely client.

About

Multithreaded DOM is possible.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 61.8%
  • TypeScript 37.2%
  • Other 1.0%