Giter Club home page Giter Club logo

vite-plugin-ssr-cloudflare-pages's Introduction

vite-plugin-ssr-cloudflare-pages

This is a demo project showcasing how to use vite-plugin-ssr and Cloudflare Pages (with wrangler@2).

Preview: https://vite-plugin-ssr-cloudflare-pages.pages.dev

Pages Setup

When adding your site to Cloudflare Pages, make sure to set the Build command to npm run build, and the Build output directory to /dist/assets. We run a script to bundle any server-side code into a custom worker, so Pages only needs to host the client-side (static) assets.

Custom Worker

This is the same worker that is used in the vite-plugin-ssr demo, but in the module format instead of the service worker format. It passes any /asset/* requests to the env.ASSETS.fetch worker helper, and any page requests are handled by vite-plugin-ssr. It is located in /worker/index.ts, and bundled into the required /dist/_worker.js file by the esbuild script (located at ./build.js) - this step is run by the npm run build command, after the site has been built by Vite.

Development

We are using the same setup as the basic vite-plugin-ssr template, with an Express server handling our site in development. This is so we can use the Vite middleware, and not have to re-bundle and run the worker everytime we change out code.

Previewing Locally

You can preview the site locally (as if it were deployed) using wrangler@2 - this is a new version of the Wrangler CLI that adds support for Pages. Make sure you have built your site using pnpm run build, then start the preview with pnpm run serve.

You will note that we pass the path to the client-side assets directory, as we did with the Pages setup.

Potential Issues

Cloudflare workers currently have a limit of 1MB, and a decent sized website will likely go over this limit when bundling the worker (as it bundles all the necessary vite-plugin-ssr code as well). Cloudflare have mentioned increasing this limit in the future however, and there is a form you can use to request an increase if needed: https://www.cloudflare.com/en-gb/larger-scripts-on-workers-early-access/

vite-plugin-ssr-cloudflare-pages's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.