Backpack Site Builder
Core Guides
ISR as default

ISR as default


Official Nextjs documentation

We decided on having ISR by default due to it being a middle ground between Server Side Rendering and Static Site Generation.

With ISR, pages get their cache invalidated every x amount of time and marks it as stale. Once someone visits the site outside of that time window, Nextjs regenerates the page for the next visitor with the latest content.

ISR strategies

No revalidation time + revalidate on demand - highly recommended

By not setting a revalidate time but ensuring we have a revalidation endpoint that revalidates pages on demand when they are published in storyblok, we can ensure maximum performance.

Inside getStaticProps, you do not need to specify revalidate to use on-demand revalidation. If revalidate is omitted, Next.js will use the default value of false (no revalidation) and only revalidate the page on-demand when revalidate() is called.


ISR on demand docs

How to add revalidation endpoint to nextjs with Storyblok


_36
import type { NextApiRequest, NextApiResponse } from 'next';
_36
import * as crypto from 'crypto';
_36
_36
const Revalidate = async (req: NextApiRequest, res: NextApiResponse) => {
_36
// Check for secret to confirm this is a valid request
_36
try {
_36
verifySignature(req.body, req.headers['webhook-signature']);
_36
} catch {
_36
return res.status(401).json({ message: 'Invalid token' });
_36
}
_36
_36
try {
_36
const { full_slug } = req.body;
_36
await res.revalidate(`/${full_slug}/`);
_36
return res.json({ revalidated: true });
_36
} catch (err) {
_36
// If there was an error, Next.js will continue
_36
// to show the last successfully generated page
_36
return res.status(500).send('Error revalidating');
_36
}
_36
};
_36
_36
const verifySignature = (body, signature) => {
_36
// webhookSecret configured in Storyblok settings
_36
const webhookSecret = process.env.STORYBLOK_PREVIEW_SECRET;
_36
let bodyHmac = crypto
_36
.createHmac('sha1', webhookSecret)
_36
.update(JSON.stringify(body))
_36
.digest('hex');
_36
_36
if (bodyHmac !== signature) {
_36
throw new Error('Signature mismatch!');
_36
}
_36
};
_36
_36
export default Revalidate;

Go to webhooks in storyblok settings and make sure to create a webhook secret. Point it to your app /api/webhooks/revalidate and it should be working from then on.

Slow revalidation times - recommended

By having a slow revalidation time (1 day for example), we can ensure content still gets regenerated quickly and don’t lose any performance. There will be 1 person per day that will get the page server side rendered only.

Recommended value revalidate: 86400 .

Fast revalidation times - not recommended

With a fast revalidation time, you get the newest content quickly but sacrifice in performance. Once the cache is marked as stale, Nextjs has to render the page completely for the next visitor.

Imagine you have a visitor every second and a revalidate time of 10 seconds. After those 10 seconds, the 11th visitor comes to your page and cache gets marked as stale. The 12th visitor will come in and get an SSR version of the page which then gets cached for the next 10 seconds again.

We don’t like having fast revalidation time with marketing sites because the content is not time critical.