Link Search Menu Expand Document

Server-side dependency sharing

Table of contents

  1. Route or Middleware based dependencies
    1. Why middleware?
  2. Example
  3. Applications without bundler
  4. Next

Depending on your usage, server-side dependency sharing could be simple or more involved. Here we’ll focus on what is believed to be the most common two usages.

Route or Middleware based dependencies

The idea here is to dynamically load each HTTP route handler dynamically from external bundles. Since we’re not bundling the entire application, only the routes themselves will become radpack’ified and gain the many benefits of shared dependencies.

Example

import middleware from '@radpack/server/middleware';

radpack.register(process.env.RADPACK_URL);

const getRadpack = middleware(radpack);

app.get('/', [getRadpack], async (req, res) => {
  // Important: Use radpack provided by middleware to support overrides
  const { default: handler } = await res.locals.radpack('radpack-rollup-server-example/home');
  return handler(req, res);
});

Why middleware?

Not required, but by leveraging radpack’s middleware utility you gain the benefit of the local proxy as well as ability to provide a ?radpack=URL querystring override which is super useful when doing soft deploys that you wish to test.

Example

Also be sure to check out examples/apps/rollup for a working example.

Applications without bundler

If you have no desire to use a bundler with your server code, “vanilla” radpack is certainly an option. Which brings us to our next topic, applications without a bundler.

Next

See how applications without a bundler work.