sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.69k stars 1.93k forks source link

adapter-netlify edge function caching support #10707

Open HexaCubist opened 1 year ago

HexaCubist commented 1 year ago

Describe the problem

One feature of Netlify edge functions is the ability to cache responses, described here: https://docs.netlify.com/edge-functions/optional-configuration/#response-caching

This requires a small change to the manifest to indicate manual caching control:

Opt in to caching
You can set the cache property to manual inline in the function code, or in netlify.toml.

For inline configuration, use the config object in your edge function file.

import type { Config } from "https://edge.netlify.com"

export default async () => new Response("Hello, world!")

export const config: Config = {
  cache: "manual",
  path: "/hello"
}

Describe the proposed solution

From what I understand, if the adapter allowed some level of configurability of the manifest output it would be possible to enable caching here:

https://github.com/sveltejs/kit/blob/c9a99b6c911c2ea9c919946aaada4f44293bdb75/packages/adapter-netlify/index.js#L116

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

ctmm commented 1 year ago

Would also benefit from having this feature implemented. Currently patching it by running a script after building the application.

package.json:

"scripts": {
    "build": "vite build && node patch-manifest.js",
// patch-manifest.js
import * as fs from 'fs';
const manifestPath = '.netlify/edge-functions/manifest.json';

try {
  const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
  manifest.functions = manifest.functions?.map((item) => ({ ...item, cache: 'manual' }));
  const patchedManifest = JSON.stringify(manifest, null, 0);
  fs.writeFileSync(manifestPath, patchedManifest, 'utf8');
  console.log('Successfully patched manifest.json cache field.');
} catch (error) {
  console.error(`Error patching manifest.json: ${error}`);
}