npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

next-baked-initial-props

v1.3.0

Published

Running multiple api fetches in getInitialProps can get slow. Sometimes you just want to save the results somewhere and fetch that instead. Next baked initial props gives you the posibility to save/bake your result to your prefered storage (JSON, or your

Readme

Next.js Baked getInitialProps

Running multiple api fetches in getInitialProps can get slow. Sometimes you just want to save the results somewhere and fetch that instead. Next.js baked initial props gives you the posibility to save/bake your result to your prefered storage (JSON, or your own storage implementation) and fetch the baked data instead of running your slow getInitialProps again and again.

Install

npm install --save next-baked-initial-props

Usage with JSON storage

The easiest wat to get started is to use the withJSONBakedInitialProps HOC in combination with your Next.js page component. When you export your Next.js app it will automaticly save a json file containing the result of your getInitialProps function. The data will be saved to /out/data/${id}.json where id is a filename derived from the url's pathname.

const Page = ({baked}) => (
  <div>
    { baked }
  </div>
);

// fetch the initial data as usual with Next.js
Page.getInitialProps = (ctx) => {
  return {
    'baked': 'nextjs'
  }
};

// Implemnent the JSON baked HOC
export default withJSONBakedInitialProps(Page)

Overriding the Id

Overriding the Id is as easy as implementing the defaultBakedInitialPropsIdentifier callback on your pageComponent. This mimics the default Identifier for example

//Generate a unique identifier for your page data
Page.getBakedInitialPropsIdentifier = (ctx) => {
    if (ctx.asPath === '/') {
        return 'index';
    }
    return ctx.asPath.replace(/^\/|\/$/g, '').replace('/', '-').toLowerCase();
};

Custom storage implementation

Don't want to use the JSON implementation and need a differen storage? This can be done by using the withBakedInitialProps HOC. This same HOC is used in the JSON implementation.

Example custom JSON implementation with withBakedInitialProps


const Page = ({baked}) => (
  <div>
    { baked }
  </div>
);

// fetch the initial data as usual with Next.js
Page.getInitialProps = (ctx) => {
  return {
    'baked': 'nextjs'
  }
};

//Generate a unique identifier for your page data
Page.getBakedInitialPropsIdentifier = (ctx) => {
    if (ctx.asPath === '/') {
        return 'index';
    }
    return ctx.asPath.split('/').slice(1).join('_').toLowerCase();
};

//Write the JSON file based on the getInitialProps
Page.bakeInitialProps = async(initialPropsData, ctx) => {
    const id = Page.getBakedInitialPropsIdentifier(ctx);
    const fs = require('fs-extra');
    fs.ensureDirSync('out/static/data');
    fs.writeJSONSync(`out/static/data/${id}.json`, initialPropsData);
};

//Fetch the baked props
Page.getBakedInitialProps = async(ctx) => {
    const id = Page.getBakedInitialPropsIdentifier(ctx);
    const response = await fetch(`/static/data/${id}.json`);
    const data = await response.json();
    return data;
};

// Implement the base HOC
export default withBakedInitialProps(Page)

Override bake triggers.

Sometimes you want to override the baking functionality for Example prismic.io has a preview feature which would be broken if we always render the baked data. withBakedInitialProps and defaultBakedInitialPropsIdentifier have a optional options parameter with following callbacks:

  • bakeWhen(defaultValue, ctx) returns a boolean which is true when the baking should happen. By default it return's true when running next export you could write your own check to let it run on next build for example.
  • getInitialPropsWhen(defaultValue, ctx) returns a boolean which is true when the intialProps data should be returned instead of the bakedData By default it return's false after your app is exported. So basicly it would only happen on export.

Example prismic preview implementation:

// when the prismic cookie exists override the baked data and use the getInitialProps value instead.
withJSONBakedInitialProps(Page, {
  getInitialPropsWhen: (defaultValue, ctx) => {
    const onPreview = typeof window !== 'undefined' && document.cookie.indexOf('io.prismic.preview') >= 0
    return defaultValue || onPreview;
  }
})