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

@lomray/vite-ssr-boost

v4.0.0

Published

Vite plugin for create awesome SSR or SPA applications on React.

Downloads

1,126

Readme

Key features:

  • Develop ⚡charged⚡ server side applications with React streaming 💨 support.
  • Unlocks Suspense for server side applications.
  • Switch between SPA and SSR in 1 second.
  • Charged CLI out of box.
  • Deploy to AWS Amplify/Docker in SPA/SSR mode for 5 sec.
  • Very easy to migrate, very easy to use.
  • All the power of vite
  • All the power of react-router🛣
  • Easy-peasy develop for Capacitor

Table of contents

Getting started

The package is distributed using npm, the node package manager.

npm i --save @lomray/vite-ssr-boost

How to use

Explore template to more understand how it works or:

  1. Add plugin to vite config:
/**
 * vite.config.ts
 */

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
/**
 * Import plugin
 */
import SsrBoost from '@lomray/vite-ssr-boost/plugin';

// https://vitejs.dev/config/
export default defineConfig({
  /**
   * Change root not necessary, but more understandable
   */
  root: 'src',
  publicDir: '../public',
  build: {
    outDir: '../build',
  },
  /**
   * Put here
   */
  plugins: [SsrBoost(), react()],
});
  1. Create client entrypoint:
/**
 * src/client.tsx
 */
import entryClient from '@lomray/vite-ssr-boost/browser/entry';
import App from './App.tsx'
import routes from './routes';

void entryClient(App, routes, {
  /**
   * (optional). Client configuration 
   */
  init: () => {},
  /**
   * (optional). Configure router options
   * @see createBrowserRouter second arg
   */
  routerOptions: {},
  /**
   * (optional). Customization create router function, e.g. use from sentry one
   */
  createRouter: createBrowserRouter,
  /**
   * (optional). Change root id
   */
  rootId: 'root',
});
  1. Create server entrypoint:
/**
 * src/server.ts
 */
import entryServer from '@lomray/vite-ssr-boost/node/entry';
import App from './App';
import routes from './routes';

export default entryServer(App, routes, {
  /**
   * Request timeout (If your backend is slow, increase this value)
   */
  abortDelay: 15000, // default: 15000 (ms)
  /**
   * Server configuration (optional)
   */
  init: () => ({
    /**
     * (optional). Called once after express server creation.
     * E.g. use for configure express middlewares
     */
    onServerCreated: () => {},
    /**
     * (optional). Called once after express server started.
     */
    onServerStarted: () => {},
    /**
     * (optional). Called on each incoming request.
     * E.g. configure request state, create state manager etc.
     */
    onRequest: async () => {},
    /**
     * (optional). Called when react router and it's context was created.
     * E.g. here you can switch stream depends on req.headers, for search crawlers you can disable stream.
     */
    onRouterReady: () => {},
    /**
     * (optional). Called when application shell is ready to send on client.
     * E.g. here you can modify header or footer.
     */
    onShellReady: () => {},
    /**
     * (optional). Called when application shell or suspense resolved and sent to the client.
     * E.g. here you can add some payload like custom state (any manager state) to response. 
     */
    onResponse: () => {},
    /**
     * (optional). Stream error callback. Catch stream errors.
     */
    onError: () => {},
    /**
     * (optional). Called when application shell or all html (depends on stream option) is ready to send on client.
     * E.g. here you can send any context or state to client.
     */
    getState: () => {},
  }),
  /**
   * (optional). Customize production log handler
   * @see @lomray/vite-ssr-boost/services/logger
   */
  loggerProd: new Logger(),
  /**
   * (optional). Customize development log handler
   * @see @lomray/vite-ssr-boost/services/logger
   */
  loggerDev: new Logger(),
  /**
   * (optional). Configure pre-builded middlewares
   */
  middlewares: {
    /**
     * @see CompressionOptions from 'compression' package
     */
    compression: {},
    /**
     * @see ServeStaticOptions from 'serve-static' package
     */
    expressStatic: {},
  },
  /**
   * (optional). Configure router static handler options
   * @see createStaticHandler second arg
   */
  routerOptions: {},
});
  1. Replace package.json scripts:
{
  ...
  "scripts": {
    "develop": "ssr-boost dev",
    "build": "ssr-boost build",
    "start:ssr": "ssr-boost start",
    "start:spa": "ssr-boost start --only-client",
    "preview": "ssr-boost preview"
  },
  ...
}
  1. Let's do the magic:
npm run develop

Plugin options

import SsrBoost from '@lomray/vite-ssr-boost/plugin';
import type { FCRoute } from '@lomray/vite-ssr-boost/interfaces/fc-route';

/**
 * Configuration
 */
SsrBoost({
  /**
   * index.html file path
   */
  indexFile: 'index.html', // default: index.html
  /**
   * Server entrypoint file
   */
  serverFile: 'server.ts', // default: server.ts
  /**
   * Client entrypoint file
   */
  clientFile: 'client.ts', // default: client.ts
  /**
   * Add tsconfig aliases to vite config aliases
   */
  tsconfigAliases: true, // default: true
  /**
   * Path contains routes declaration files (need to detect route files).
   */
  routesPath: undefined, //default: undefined
  /**
   * Create additional SPA entrypoint: index-spa.html
   * Can be used for service worker: createHandlerBoundToURL("index-spa.html")
   */
  spaIndex: false, // default: false
  /**
   * Additional entry points for build
   */
  entrypoint: [], // default: undefined
})

Useful imports

/**
 * Components
 */
// Navigate component based on react-router-dom with server-side support
import Navigate from '@lomray/vite-ssr-boost/components/navigate';
// Change server response status
import ResponseStatus from '@lomray/vite-ssr-boost/components/response-status';
// Scroll page to top after navigate
import ScrollToTop from '@lomray/vite-ssr-boost/components/scroll-to-top';
// HOC for wrap component in Suspense
import withSuspense from '@lomray/vite-ssr-boost/components/with-suspense';
// Only client side components
import OnlyClient from '@lomray/vite-ssr-boost/components/only-client';

/**
 * Helpers
 */
// Get server state (e.g. state manager) on client side
import getServerState from '@lomray/vite-ssr-boost/helpers/get-server-state';

/**
 * Interfaces
 */
// interfaces for route components
import type { FCRoute, FCCRoute } from '@lomray/vite-ssr-boost/interfaces/fc-route';
// interface for define routes
import type { TRouteObject } from '@lomray/vite-ssr-boost/interfaces/route-object';

Client side components import example:

<OnlyClient load={() => import('external-package')}>
  {(LoadedComponent) => (
    <LoadedComponent />
  )}
</OnlyClient>

CLI

Explore all commands and options:

ssr-boost -h

WARNING

Route imports of the following types are supported:

import { RouteObject } from 'react-router-dom';
import HomePage from './pages/home'; // not lazy imports should be directly in file where it use

const importPath = './pages/home';

const routes: RouteObject[] = [
  {
    path: '/home',
    Component: HomePage, // support
    element: <AppLayout />, // support
    lazy: () => import('./pages/home'), // support
    lazy: () => import(importPath), // not support, but you can move logic in separate file and import it with supported case
    lazy: () => { // not support
        return import('./pages/home');
    } 
  }
];

Use Cases

Change basename

/**
 * Configure client
 */
void entryClient(App, routes, {
  routerOptions: {
    basename: '/custom',
  },
});
/**
 * Configure server
 */
export default entryServer(App, routes, {
  routerOptions: {
    basename: '/custom',
  },
});

Change base for static assets

// https://vitejs.dev/config/
export default defineConfig({
  base: '/static',
});
/**
 * Configure server
 * NOTE: 'basename' should be equal to 'base' from vite config 
 */
export default entryServer(App, routes, {
  middlewares: {
    expressStatic: {
      basename: '/static',
    },
  },
});

Capacitor additional endpoint

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    SsrBoost({
      entrypoint: [
        {
          name: 'mobile',
          type: 'spa',
          clientFile: './src/mobile.tsx',
          buildOptions: '--mode mobile',
        },
      ],
    }),
    react(),
  ],
});
const AppMobile: FC = (props) => {
  // some mobile logic

  return <App {...props} />;
}

/**
 * src/mobile.tsx
 */
void entryClient(AppMobile, routes, {});

Bugs and feature requests

Bug or a feature request, please open a new issue.

License

Made with 💚

Published under MIT License.