9/18 | Validate feature ideas earlier with AI-driven prototypes

What are best AI tools? Take the State of AI survey

Builder.io
Builder.io
Contact sales

9/18 | Validate feature ideas earlier with AI-driven prototypes

What are best AI tools? Take the State of AI survey

Builder.io
Builder.io
< Back to blog

Query Params & Cookies

Using Query Params and Cookies in Next.js Static Pages

October 7, 2022

Written By Aziz Abbas

Next.js incremental static regeneration is fantastic—it allows for delivering pre-built static pages at scale, rather than rebuilding the entire site on every deployment (cough, cough, Gatsby!). You may scale to millions of static pages while maintaining the advantages of SSG.

Static pages are built in a non-blocking way:

  • During build time
  • On revalidation hooks calls
  • On the first request after the revalidation duration is over

Because of this, developers can't out-of-the-box build pages that depend on the request context. For example, if your page design is different for logged-in users, return visitors, geographical location, etc., you'd have to render those elements on the client...but there is a solution.

Next.js Edge Middleware to the rescue

With the help of the middleware provided by Next.js, you can write functions that run halfway between the user's request initiation and completion. This way you can process a user's request and rewrite it to a static page if needed.

So in our case, we want to rewrite:

To:

Where path is a deterministic string representation of the various options your static page needs to render accurately.

So, you'd use the middleware to encode things from the request context; such as, geo location, return visitor cookies, and query params. And on the other end you'd have your static path handler decode it.

For example, you could use encodeOptions for whether or not the user is a return visitor, getting their country, and getting the page:

Then in your static path handler you can decode these options:

Now we can implement our encoding/decoding functions, which could be as basic as using JSON.stringify . But we can do better than that—let's use a library that produces deterministic strings from an object:

npm install fast-json-stable-stringify

And use it as in this snippet:

Conclusion

Combining Next.js middlewares with SSG/ISR pages is super powerful and opens up a variety of use cases, such as:

  • Automatic localization of content based on geographic location, by looking at the request.geo or the Accept-Language headers.
  • Changing themes of the site based on the hosting domain, by looking at the request.headers.get('host').
  • A/B testing variations of the same page.
  • Personalizing experiences based on user type.

For more on how our customers use the same technique to deliver high-performing A/B testing and content personalization with Builder.io, check out A/B Testing and Personalization with Next.js Edge Middleware.

Share

Twitter
LinkedIn
Facebook
Share this blog
Copy icon
Twitter "X" icon
LinkedIn icon
Facebook icon

Visually edit your codebase with AI

Using simple prompts or Figma-like controls.

Try it nowGet a demo

Design to Code Automation

A pragmatic guide for engineering leaders and development teams


Continue Reading
design9 MIN
How to generate (actually good) designs with AI
September 17, 2025
AI9 MIN
7 Levels of Context Engineering for Designers
September 16, 2025
Design to Code8 MIN
Git Branching for Designers
September 11, 2025

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Resources

User Guides

Developer Docs

Forum

Blog

Github

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Get the latest from Builder.io

By submitting, you agree to our Privacy Policy

  • Fusion

  • Publish

  • Product Updates

  • Design to Code

  • Headless CMS

    Multi-Brand CMS

  • Landing Pages

  • Web Apps

  • Prototypes

  • Marketing Sites

  • Headless Commerce

  • Documentation

  • Fusion Docs

  • Publish Docs

  • Blog

  • Webinars

  • Guides

  • Case Studies

  • Community Forum

  • Partners

  • Affiliate Program

  • CMS Integrations

  • CMS Blueprints

  • Glossary

  • Figma to Code Guide

  • Headless CMS Guide

  • Headless Commerce Guide

  • Composable DXP Guide

  • About

  • Careers

  • Contact Sales

Security

Privacy Policy

SaaS Terms

Compliance

Cookie Preferences

YouTube icon
Github icon
Blsky Icon
Twitter "X" icon
LinkedIn icon
Feed Icon
Gartner Cool Vendor 2024