This project demonstrates building a web application using vanilla Web Components, powered by Vite, with support for both Single Page Application (SPA) and Server-Side Rendering (SSR) modes. It features CSS Modules for scoped styling, a custom global state management system, and client-side routing.
- Vanilla Web Components: Build reusable UI components with native browser capabilities.
- Vite: Fast development server and optimized build process.
- CSS Modules: Locally scoped CSS for components, preventing style conflicts.
- Custom Global State Management: A simple
storefor managing application-wide state. - Client-Side Routing: Smooth page transitions without full page reloads.
- Server-Side Rendering (SSR): Improved initial load performance and SEO.
- Netlify SSR Support: Configured for deployment on Netlify using Functions.
First, install the project dependencies:
yarnThis project supports two main modes of operation: Single Page Application (SPA) and Server-Side Rendering (SSR).
In SPA mode, the entire application is rendered client-side. This is the traditional way a modern web app operates after the initial HTML load.
- Development Server (SPA): Runs a development server for client-side development with Hot Module Replacement (HMR).
yarn dev-spa
- Build (SPA): Creates a production-ready client-side bundle in the
dist/clientdirectory.yarn build-spa
- Preview (SPA): Serves the production client-side build locally for testing.
yarn preview-spa
In SSR mode, the initial page request is rendered on the server, improving perceived performance and SEO. The application then "hydrates" on the client, becoming a fully interactive SPA.
- Development Server (SSR): Runs a Node.js server that integrates with Vite's dev server to provide SSR with HMR.
yarn dev
- Build (SSR): Creates separate production bundles for the client (
dist/client) and the server (dist/server). This command must be run beforeyarn start.yarn build
- Start (SSR Production): Runs the Node.js server in production mode, serving the pre-built client and server bundles.
yarn start
This project is configured to be deployed on Netlify, supporting both SPA and SSR modes.
To deploy the SPA version:
- Run
yarn build-spa. - Connect your repository to Netlify.
- Configure Netlify to:
- Build command:
yarn build-spa - Publish directory:
dist/client - Fallback/Redirects: Add a
_redirectsfile or Netlify UI rule to redirect all unmatched paths toindex.html(for client-side routing).
- Build command:
This project is pre-configured for Netlify SSR deployment using Netlify Functions.
netlify/
functions/
ssr.mjs # SSR handler function
routes.mjs # Routes API function
netlify.toml # Netlify configuration
-
Build:
yarn buildoutputs to:dist/client/- Static assets (HTML, JS, CSS, components)dist/server/- Server-side code for SSR
-
Static Assets: Requests to
/core/*,/lib/*,/components/*,/app/*are served directly fromdist/client/ -
SSR: All other routes go through the
ssrfunction which:- Renders the page server-side
- Handles auth redirects (302)
- Returns fully rendered HTML
-
Routes API:
/api/routesis handled by theroutesfunction
- Connect your repository to Netlify
- Netlify auto-detects
netlify.tomlconfiguration:- Build command:
yarn build - Publish directory:
dist/client - Functions directory:
netlify/functions
- Build command:
- Push to trigger deployment
netlify.toml:
[build]
command = "yarn build"
publish = "dist/client"
functions = "netlify/functions"
[functions]
node_bundler = "esbuild"
# Static assets served directly
[[redirects]]
from = "/core/*"
to = "/core/:splat"
status = 200
# ... more static redirects ...
# SSR for all other routes
[[redirects]]
from = "/*"
to = "/.netlify/functions/ssr"
status = 200To test the Netlify setup locally, install the Netlify CLI:
npm install -g netlify-cli
netlify dev