question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support on-demand purge nextjs cache released in 12.1

See original GitHub issue

Is your feature request related to a problem? Please describe. Without the mechanism to purge the nextjs cache, it is a pitfall in production use and causing a lot of problems.

Describe the solution you’d like Support the res.unstable_revalidate in api which can be controlled by backend to purge the cache and regenerate the page requested. It is supported in Nextjs 12.1, and it will be great if we can support it as well: https://nextjs.org/blog/next-12-1

Describe alternatives you’ve considered Disable the next.js cache totally. To be tested. Using CDN Cache instead with a purge api call.

Additional context An example is in this video: https://www.youtube.com/watch?v=BGexHR1tuOA&t=16s

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:11
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
jlaramiecommented, Apr 5, 2022

This is the error I get when I tried to call res.unstable_revalidate

Error: Failed to revalidate /: Invariant: required internal revalidate method not passed to api-utils

3reactions
kigorwcommented, May 4, 2022

I was able to trigger regeneration. Feel free to adapt this code to your needs until we get some better official approach.

import { triggerStaticRegeneration } from '@sls-next/lambda-at-edge/dist/lib/triggerStaticRegeneration'
import { defaultRegion, siteSQS, websiteBucket } from '../../config'
import * as s3 from './s3'

export async function getBuildId(): Promise<string> {
  const x = await s3.get('BUILD_ID', websiteBucket)

  return (x || '').toString()
}

export function getPageKey(buildId: string, key: string) {
  return `${getStaticPagesPath(buildId)}/${key}.html`
}
export function getStaticPagesPath(buildId: string) {
  return `static-pages/${buildId}`
}

export function getRegenerationOptions({
  queueName,
  buildId,
  page,
  lang,
  bucket,
  region,
  counter
}: {
  queueName: string
  buildId: string
  page: string
  lang: 'en' | 'ka'
  bucket: string
  region: string
  counter: number
}) {
  return {
    basePath: '',
    pagePath: 'pages/' + (page || 'index') + '.js',

    pageS3Path: '/' + lang + (page ? '/' + page : '') + '.html',
    storeName: bucket,
    storeRegion: region,
    request: {
      uri: '/' + lang + (page ? '/' + page : ''),
      origin: {
        s3: {
          region,
          domainName: `${bucket}.s3.${region}.amazonaws.com`,
          path: '/' + getStaticPagesPath(buildId)
        }
      }
    } as AWSLambda.CloudFrontRequest,
    eTag: Date.now() - 1000 + counter,
    lastModified: Date.now() - 1000 + counter,
    queueName
  }
}

export async function regeneratePage(lang: 'en' | 'ka', page: string, counter = 0) {
  const buildId = await getBuildId()
  console.log('queue', siteSQS)

  const options = getRegenerationOptions({
    queueName: siteSQS,
    buildId,
    lang,
    page,
    bucket: websiteBucket,
    region: defaultRegion,
    counter
  })

  console.log(options)

  const x = await triggerStaticRegeneration(options as any)

  console.log('event', x)
}


Read more comments on GitHub >

github_iconTop Results From Across the Web

Blog - Next.js 12.1
You can now manually purge the Next.js cache for a specific page on-demand. This makes it easier to update your site when: Content...
Read more >
How to clear/delete cache in NextJs? - Stack Overflow
I think this is possible starting from next@12.1.x using this feature On-demand Incremental Static Regeneration ...
Read more >
Tim on Twitter: "Image optimization now automatically uses ...
Next.js 12.1 : ◇ On-demand Incremental Static Regeneration ◇ SWC support ... Image optimization now automatically uses the same cache as "revalidate" in ......
Read more >
Question - timeline to support on-demand Incremental Static ...
Next 12.1.0 was released in the last few days and it provides support for On-demand Incremental Static Regeneration (ISR): Blog - Next.js ......
Read more >
Incremental Static Regeneration with Next.js - LogRocket Blog
Next.js v9.5 introduced a new strategy called Incremental Static ... In Next.js 12, we have another feature called on-demand ISR, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found