1.0.3 • Published 7 months ago

@iampersoncool/simple-ssr v1.0.3

Weekly downloads
-
License
-
Repository
-
Last release
7 months ago

Simple SSR library

Notes

  • The vite plugin will read the viewsDir from the view engine and recursively add all the files in the directories to the vite config's build.rollupOptions.input.
  • For all view engines, you MUST set the file extension to .html, because vite throws an error if it does not recognize the file extension as html, javascript, or typescript. This also means you must also write your templates in html files, which should be fine.
  • If you are using ejs or eta, you cannot use the default delimiters (<% and %>) because vite thinks these are html tags, which it does not recognize, throwing an error. The EtaViewEngine supports changing delimiters, as shown in the example usages. You can change it to anything you want, I recommend something like {% and %}.
  • The viewsDir when you create a instance of ViewEngine has to be relative to your vite config's root. For example, if your root was at ./client and your views was in that folder, you would put 'views'.

Example directory structure

root
  ├── server.ts
  ├── config.ts
  └── vite.config.ts

Example server

server.ts

import FastifySimpleSSR from '@iampersoncool/fastify-simple-ssr/FastifySimpleSSR';
import { engine } from './config.js';

import fastify from 'fastify';

const ssr = new FastifySimpleSSR();

const app = fastify();

const middleware =
  process.env.NODE_ENV === 'production'
    ? ssr.getProdMiddleware()
    : ssr.getDevMiddleware();

app.register(middleware, {
  viewEngine: engine,
});

Example usage(EdgeViewEngine)

config.ts

import EdgeViewEngine from '@iampersoncool/simple-ssr-edge';

export const engine = new EdgeViewEngine(
  'views',
  ['{{', '}}'],
  '.html',
  process.env.NODE_ENV === 'production'
);

Example usage(EtaViewEngine)

config.ts

import EtaViewEngine from '@iampersoncool/simple-ssr-eta';

export const engine = new EtaViewEngine(
  'views',
  ['{%', '%}'],
  '.html',
  process.env.NODE_ENV === 'production'
);

Vite config

vite.config.ts

import SimpleSSRVitePlugin from '@iampersoncool/simple-ssr/vite-plugin';
import { defineConfig } from 'vite';
import { engine } from './config';

export default defineConfig({
  root: 'client',
  build: {
    outDir: 'dist',
  },
  plugins: [SimpleSSRVitePlugin({ viewEngine: engine })],
});
1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago