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

fix: edge runtime dynamic code eval #1831

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/next-sanity/package.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const MODULE_PATHS_WHICH_USE_SERVER_DIRECTIVE_SHOULD_BE_ADDED = [

export default defineConfig({
tsconfig: 'tsconfig.build.json',
bundles: [
{
source: './src/index.edge-light.ts',
import: './dist/index.edge-light.js',
},
],
rollup: {
output: {
banner: (chunkInfo) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/next-sanity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-sanity",
"version": "9.8.2",
"version": "9.8.3-canary.1",
"description": "Sanity.io toolkit for Next.js",
"keywords": [
"sanity",
Expand All @@ -27,6 +27,7 @@
"exports": {
".": {
"source": "./src/index.ts",
"edge-light": "./dist/index.edge-light.js",
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
Expand Down
16 changes: 16 additions & 0 deletions packages/next-sanity/src/index.edge-light.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Some of the exports on index.ts causes errors on the edge runtime, so we omit them here.
*/

import type {VisualEditingProps} from './visual-editing'

export * from './client'
export * from '@portabletext/react'
export * from '@sanity/next-loader'
export {defineQuery, default as groq} from 'groq'
export type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function VisualEditing(_props: VisualEditingProps): React.ReactNode {
throw new TypeError('VisualEditing is not supported on the edge runtime')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
*
* If pages router supported `next/dynamic` imports (it wants `next/dynamic.js`),
* or if turbopack in app router allowed `next/dynamic.js` (it doesn't yet)
* we could use `dynamic(() => import('...), {ssr: false})` here.
* Since we can't, we need to use a lazy import and Suspense ourself.
*/

import {lazy, Suspense} from 'react'

import type {VisualEditingProps} from './VisualEditing'

const VisualEditingClientComponent = lazy(() => import('./VisualEditing'))

export function VisualEditingLazyClientComponent(props: VisualEditingProps): React.ReactNode {
return (
<Suspense fallback={null}>
<VisualEditingClientComponent {...props} />
</Suspense>
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use client'

export type {VisualEditingProps} from './VisualEditing'
export {default} from './VisualEditing'
export {VisualEditingLazyClientComponent as default} from './VisualEditingLazy'
4 changes: 1 addition & 3 deletions packages/next-sanity/src/visual-editing/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import dynamic from 'next/dynamic'
import type {VisualEditingProps} from 'next-sanity/visual-editing/client-component'

const VisualEditingComponent = dynamic(() => import('next-sanity/visual-editing/client-component'))
import VisualEditingComponent from 'next-sanity/visual-editing/client-component'

/**
* @public
Expand Down
Loading