Giter Site home page Giter Site logo

atilafassina / xata-nextjs-getstaticparams Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 44 KB

reproducing an issue with `getStaticParams` and caching

Home Page: https://xata-nextjs-getstaticparams.vercel.app

TypeScript 44.19% CSS 54.74% JavaScript 1.07%

xata-nextjs-getstaticparams's Introduction

This is contrived issue reproduction between Next.js and Xata.

Issue ๐ŸŒ‹

When using Xata in a dynamic route with getStaticParams it will cause a runtime shift between first build and rebuilds. This happens because incrementalCache seems to switch values from first build.

โ—๏ธ Issue happens only in development

Deployed app: xata-nextjs-getstaticparams.vercel.app

Workaround

Passing a hardcoded next.revalidate option on build will stick it to either dynamic or static.

Dynamic
const xata = new XataClient({
    fetch: (path, options) => {
      return fetch(path, {
        ...options,
        next: {
          revalidate: false,
        },
      });
    },
  });
Static
const xata = new XataClient({
  fetch: (path, options) => {
    return fetch(path, {
      ...options,
      next: {
        revalidate: 10,
      },
    });
  },
});
Environment Based
const xata = new XataClient({
  fetch: (path, options) => {
    return fetch(path, {
      ...options,
      next: {
        revalidate: process.env.NODE_ENV === "development" ? 0 : 60,
      },
    });
  },
});

The problem with the workaround is that the user will need to always generate their own instance instead of leveraging the codegen to its maximum potential.

Ideal Solution

The SDK can uses the fetch instance from Next.js without needing to hardcode any default value.

Run App

pnpm i && pnpm dev

Serves in localhost:3000

Environment Variable

This app requires a Xata account, once you have one add your access token to your .env or .env.local to run it locally.

XATA_API_KEY=<token>

Database

The .xatarc carries the build configuration to your app. codegen.output will tell the CLI where to put the generated SDK code. While the databaseURL is your Database URL. You can generate it using the Xata CLI, manually, or via the VS Code Extension.

๐Ÿ’ก It is also possible to pass --databaseUrl and --output flags to the xata:codegen task and skip the .xatarc completely.

Minimal Schema

Posts

Column Type
id string
slug string
title string

๐Ÿ’ก You can use the Example Database on Xata Web UI.


Thanks to @Sam-Apostel for first finding this at sam-apostel/blog

xata-nextjs-getstaticparams's People

Contributors

atilafassina avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.