How to use fresh - 6 common examples

To help you get started, we’ve selected a few fresh examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github avetjs / avet / core / server / render.js View on Github external
export function sendHTML(req, res, html, method, { dev }) {
  if (res.finished) return;
  const etag = generateETag(html);

  if (fresh(req.headers, { etag })) {
    res.statusCode = 304;
    res.end();
    return;
  }

  if (dev) {
    // In dev, we should not cache pages for any reason.
    // That's why we do this.
    res.setHeader('Cache-Control', 'no-store, must-revalidate');
  }

  res.setHeader('ETag', etag);
  res.setHeader('Content-Type', 'text/html');
  res.setHeader('Content-Length', Buffer.byteLength(html));
  res.end(method === 'HEAD' ? null : html);
}
github lnlfps / symph-joy / server / render.js View on Github external
export function sendHTML (req, res, html, method, { dev, generateEtags }) {
  if (isResSent(res)) return
  const etag = generateEtags && generateETag(html)

  if (fresh(req.headers, { etag })) {
    res.statusCode = 304
    res.end()
    return
  }

  if (dev) {
    // In dev, we should not cache pages for any reason.
    // That's why we do this.
    res.setHeader('Cache-Control', 'no-store, must-revalidate')
  }

  if (etag) {
    res.setHeader('ETag', etag)
  }

  if (!res.getHeader('Content-Type')) {
github nuxt / nuxt.js / packages / server / src / middleware / nuxt.js View on Github external
preloadFiles
    } = result

    if (redirected) {
      await nuxt.callHook('render:routeDone', url, result, context)
      return html
    }
    if (error) {
      res.statusCode = context.nuxt.error.statusCode || 500
    }

    // Add ETag header
    if (!error && options.render.etag) {
      const { hash } = options.render.etag
      const etag = hash ? hash(html, options.render.etag) : generateETag(html, options.render.etag)
      if (fresh(req.headers, { etag })) {
        res.statusCode = 304
        res.end()
        await nuxt.callHook('render:routeDone', url, result, context)
        return
      }
      res.setHeader('ETag', etag)
    }

    // HTTP2 push headers for preload assets
    if (!error && options.render.http2.push) {
      // Parse resourceHints to extract HTTP.2 prefetch/push headers
      // https://w3c.github.io/preload/#server-push-http-2
      const { shouldPush, pushAssets } = options.render.http2
      const { publicPath } = resources.clientManifest

      const links = pushAssets
github TaumuLu / lissom / src / server / send-html.ts View on Github external
export default function sendHTML(req, res, html: string, method: string) {
  const { dev, generateEtags } = config.get();
  if (isResSent(res)) return;
  const etag = generateEtags && generateETag(html);

  if (fresh(req.headers, { etag })) {
    res.statusCode = 304;
    res.end();
    return;
  }

  if (dev) {
    res.setHeader('Cache-Control', 'no-store, must-revalidate');
  }

  if (etag) {
    res.setHeader('ETag', etag);
  }

  if (!res.getHeader('Content-Type')) {
    res.setHeader('Content-Type', 'text/html; charset=utf-8');
  }
github poppinss / request / src / Request.ts View on Github external
public fresh (): boolean {
    if (['GET', 'HEAD'].indexOf(this.intended()) === -1) {
      return false
    }

    const status = this.response.statusCode
    if ((status >= 200 && status < 300) || status === 304) {
      return fresh(this.headers(), this.response.getHeaders())
    }

    return false
  }
github TaumuLu / lissom / src / server / server.ts View on Github external
public sendHTML(
    req: IncomingMessage,
    res: ServerResponse,
    html: string,
    method: string
  ) {
    if (isResSent(res)) return;

    const { dev, generateEtags } = config.get();
    const etag = generateEtags && generateETag(html);
    if (fresh(req.headers, { etag })) {
      res.statusCode = 304;
      res.end();
      return;
    }

    if (dev) {
      res.setHeader('Cache-Control', 'no-store, must-revalidate');
    }
    if (etag) {
      res.setHeader('ETag', etag);
    }
    if (!res.getHeader('Content-Type')) {
      res.setHeader('Content-Type', 'text/html; charset=utf-8');
    }
    res.setHeader('Content-Length', Buffer.byteLength(html));
    res.end(method === 'HEAD' ? null : html);

fresh

HTTP response freshness testing

MIT
Latest version published 7 years ago

Package Health Score

76 / 100
Full package analysis

Popular fresh functions