Documentation menuMenu da documentação

$ cat docs/middleware/cache-control.md

Cache-ControlCache-Control

Cache public pages and never let a personal response land in a shared cache by mistake.Faça cache de páginas públicas e nunca deixe uma resposta pessoal cair em um cache compartilhado por engano.

What it doesO que faz

cacheControl(options: CacheControlOptions): MiddlewareHandler. value goes on anonymous GET/HEAD responses with status 2xx or 3xx. Everything else gets no-store: requests with authorization or cookie, responses that set a cookie, writes and errors. A value the handler already set is kept.

cacheControl(options: CacheControlOptions): MiddlewareHandler. value vai nas respostas anônimas de GET/HEAD com status 2xx ou 3xx. Todo o resto recebe no-store: requisições com authorization ou cookie, respostas que definem cookie, escritas e erros. Um valor já definido pelo handler é mantido.

Install and orderInstalar e ordenar

terminal
pnpm add @tupik/middleware

Register it per module, with the value that fits those routes.

Registre por módulo, com o valor que serve para aquelas rotas.

ExampleExemplo

cache-control.ts
import { createModule } from '@tupik/core';
import { cacheControl } from '@tupik/middleware';

const pub = createModule('/public')
  .use(cacheControl({ value: 'public, max-age=300' })) // no-store when a cookie is present
  .get('/plans', { access: 'public', handler: () => listPlans() });
import { createModule } from '@tupik/core';
import { cacheControl } from '@tupik/middleware';

const pub = createModule('/public')
  .use(cacheControl({ value: 'public, max-age=300' })) // no-store quando há cookie
  .get('/plans', { access: 'public', handler: () => listPlans() });

APIAPI

OptionOpçãoTypeTipoDefault / behaviorPadrão / comportamento
value string Required. Value for anonymous, successful reads.Obrigatório. Valor para leituras anônimas com sucesso.
authenticated (ctx, response) => boolean authorization or cookie on the request, or set-cookie on the response.authorization ou cookie na requisição, ou set-cookie na resposta.

interface CacheControlOptions { value: string; authenticated? }

interface CacheControlOptions { value: string; authenticated? }

Errors and limitsErros e limites

  • An empty value or one with CR/LF throws at creation.
  • Any cookie counts as authenticated by default (analytics cookies too): pass authenticated to narrow it.
  • Valor vazio ou com CR/LF lança na criação.
  • Qualquer cookie conta como autenticado por padrão (inclusive de analytics): passe authenticated para restringir.

When not to use itQuando não usar

It does not add Vary: if the body changes with a header (language, auth), add Vary yourself before caching publicly.

Ele não adiciona Vary: se o corpo muda com um header (idioma, auth), adicione Vary antes de fazer cache público.