A full example for deploying vite-plugin-ssr
on Netlify.
Extension of https://github.com/AaronBeaudoin/vite-plugin-ssr-example.
- Fork this repository.
- Go to https://app.netlify.com/start.
- Connect to GitHub and select your fork.
- Press the "Deploy" button.
- Wait for Netlify to run the build (~30s).
- Go to the URL for your deployment.
- Enjoy! 🍹
If your pages don't need to change often, you may want to pre-render all pages to static files and deploy your site without a serverless function. To deploy this way instead:
- Delete the
functions/
directory. - Remove the
[functions]
section from yournetlify.toml
. - Change the
to
value for your "catch-all" (/*
) rewrite to/404
. - Change the
status
value for your "catch-all" (/*
) rewrite to404
. - Remove
doNotPrerender
from all pages (even_default
). - Deploy! 🚀
- Netlify deployment configuration all starts with a
netlify.toml
file at the root of the project. Netlify calls this file-based configuration because some of the options can also be set in the Netlify dashboard. - The
build.publish
option innetlify.toml
specifies the path to static files generated by the build. Forvite-plugin-ssr
, this path is thedist/client/
directory created during the build. Thebuild.command
option specifies the command we use to build our project. Forvite-plugin-ssr
, this command is simplyvite build
, which we trigger in our project via an NPM script withnpm run build
. - The
functions.directory
option innetlify.toml
specifies the path to our project's serverless functions. In our project, we simply usefunctions/
. All of our routes will be handled by a single serverless function, which is exported ashandler
fromfunctions/index.ts
. This function imports therenderPage
function fromvite-plugin-ssr
, which internally creates a dependency on thedist/server/
directory created during the build. Types for our function are provided by@netlify/functions
. - The
redirects
option innetlify.toml
is an array of redirect rules we would like to use for our project's deployment. In our project, we want all requests for which there exists no static file to be handled by our serverless function. We specify/*
for theredirects[].from
option to catch all routes. We specify/.netlify/functions/index
for theredirects[].to
option because that path is where Netlify will deploy our function as described here in the docs. Finally, we specify a200
for thestatus
option, which causes our rule to become a rewrite.
- The
netlify.toml
file contains Netlify deployment configuration. - The
functions/index.ts
file contains our serverless function code. - The
@netlify/functions
package provides types for our serverless function.
Netlify doesn't support caching in front of serverless functions, so every incoming request that isn't to a static file will invoke your serverless function. If this is what you want, great! Otherwise, check out Netlify On-demand Builders for a potential solution, with some limitations.