Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any import from next-sanity prevents Next.js static builds (output: 'export') #1899

Open
dylansmith opened this issue Nov 8, 2024 · 6 comments

Comments

@dylansmith
Copy link

dylansmith commented Nov 8, 2024

Describe the bug

We recently tried to build our Next.js (14.2.14) project using static exports via the output: 'export' config. However, the build kept failing with the error message:

> Server Actions are not supported with static export.

As the message says, you cannot render an app with Server Actions as a static site, but we were not using any server actions in our code. After hours of trying to figure out why Next was behaving this way, I isolated the issue to imports from next-sanity like these:

import { defineQuery } from 'next-sanity';

It appears as though the module packages/next-sanity/src/visual-editing/server-actions/index.ts is the culprit here. Possibly there is a chain of imports in place that whenever you import from next-sanity this module is resolved as well. In our case (as will be for many others using Sanity) our Next.js pages will run a query against Sanity on every page request so if the import from next-sanity is in this codepath then all pages will not be able to be statically generated.

I was able to workaround this issue by changing the following imports:

// import { defineQuery } from 'next-sanity';
import { defineQuery } from 'groq';

// import { groq } from 'next-sanity';
import groq from 'groq'`;

// import { createClient } from 'next-sanity';
import { createClient, SanityClient } from '@sanity/client';

To Reproduce

Steps to reproduce the behavior:

Add an import from next-sanity to any Next.js page or a module that is imported into a page, e.g.

import { defineQuery } from 'next-sanity';
import { groq } from 'next-sanity';

Expected behavior

Imports from next-sanity that are unrelated to visual editing should not break static builds in Next.js.

Which versions of Sanity are you using?

@sanity/cli (global)                   3.59.0 (latest: 3.63.0)
@sanity/document-internationalization   3.1.0 (up to date)
@sanity/image-url                       1.0.2 (latest: 1.1.0)
@sanity/vision                         3.59.0 (latest: 3.63.0)
sanity                                 3.59.0 (latest: 3.63.0)

What operating system are you using?

MacOS 13.4 (22F66)

Which versions of Node.js / npm are you running?

10.8.2
v22.6.0

Additional context

Add any other context about the problem here.

Security issue?

N/A

@giangiuliani
Copy link

+1 this is definitely an issue. The workaround works at least.

@junghyeonsu
Copy link

I think when using next output: export and using next-sanity implementations, the same error appears

@cbou
Copy link

cbou commented Dec 22, 2024

@dylansmith How have you solved sanityFetch ?

My live.ts is

import { defineLive } from "next-sanity";
import { client } from "./client";
import { token } from "./token";

export const { sanityFetch, SanityLive } = defineLive({
  client: client.withConfig({ apiVersion: "vX" }),
  browserToken: token,
  serverToken: token,
});

I tried to change import { defineLive } from "next-sanity"; by import { defineLive } from "@sanity/next-loader"; but I still get Server Actions are not supported with static export..

@EricWVGG
Copy link

EricWVGG commented Dec 30, 2024

I’m seeing this same issue.

import { createClient } from "next-sanity"

export default async function Home() {
  const res = await fetch("https://api.vercel.app/blog")
  const page = await res.json()
  // ^ this fetch works, just a "sanity" check to confirm that async fetches can build normally.

  const client = createClient({
    projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
    dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
    apiVersion: process.env.NEXT_PUBLIC_SANITY_API_VERSION,
    useCdn: true,
  })
  // ^ this breaks the build. If commented out, build is fine.
  return <p>{page.length} results</p>
}

@dylansmith’s workaround does appear to work (Next v15), so I’ll pursue that in my actual app. THANK YOU

@eivindml
Copy link

eivindml commented Jan 6, 2025

The VisualEditing is also bundled when not used, adding a lot to the bundle size.

@george-activetheory
Copy link

I'm pretty sure the problem is with this file:
https://github.com/sanity-io/visual-editing/blob/main/packages/next-loader/src/server-actions/index.ts

Not sure how that could be conditional to static exports though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants