Skip to content

Commit

Permalink
Merge branch 'canary' into api-switch-runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] committed Sep 7, 2022
2 parents 081a873 + 084ad96 commit 54e7dc5
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 46 deletions.
1 change: 0 additions & 1 deletion .github/workflows/validate_issue.yml
Expand Up @@ -15,4 +15,3 @@ jobs:
run: node ./.github/actions/issue-validator/index.mjs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEBUG: 1
31 changes: 0 additions & 31 deletions examples/with-context-api/components/Counter.js

This file was deleted.

57 changes: 57 additions & 0 deletions examples/with-context-api/components/Counter.tsx
@@ -0,0 +1,57 @@
import {
useReducer,
useContext,
createContext,
ReactNode,
Dispatch,
} from 'react'

type CounterState = number
type CounterAction =
| {
type: 'INCREASE' | 'DECREASE'
}
| {
type: 'INCREASE_BY'
payload: number
}

const CounterStateContext = createContext<CounterState>(0)
const CounterDispatchContext = createContext<Dispatch<CounterAction>>(
() => null
)

const reducer = (state: CounterState, action: CounterAction) => {
switch (action.type) {
case 'INCREASE':
return state + 1
case 'DECREASE':
return state - 1
case 'INCREASE_BY':
return state + action.payload
default:
throw new Error(`Unknown action: ${JSON.stringify(action)}`)
}
}

type CounterProviderProps = {
children: ReactNode
initialValue?: number
}

export const CounterProvider = ({
children,
initialValue = 0,
}: CounterProviderProps) => {
const [state, dispatch] = useReducer(reducer, initialValue)
return (
<CounterDispatchContext.Provider value={dispatch}>
<CounterStateContext.Provider value={state}>
{children}
</CounterStateContext.Provider>
</CounterDispatchContext.Provider>
)
}

export const useCount = () => useContext(CounterStateContext)
export const useDispatchCount = () => useContext(CounterDispatchContext)
9 changes: 7 additions & 2 deletions examples/with-context-api/package.json
Expand Up @@ -7,7 +7,12 @@
},
"dependencies": {
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "18.7.15",
"@types/react": "16.9.17",
"typescript": "4.8.2"
}
}
@@ -1,11 +1,10 @@
import type { AppProps } from 'next/app'
import { CounterProvider } from '../components/Counter'

function MyApp({ Component, pageProps }) {
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<CounterProvider>
<Component {...pageProps} />
</CounterProvider>
)
}

export default MyApp
@@ -1,15 +1,16 @@
import type { MouseEvent } from 'react'
import Link from 'next/link'
import { useCount, useDispatchCount } from '../components/Counter'

const AboutPage = () => {
const count = useCount()
const dispatch = useDispatchCount()

const handleIncrease = (event) =>
const handleIncrease = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({
type: 'INCREASE',
})
const handleIncrease15 = (event) =>
const handleIncrease15 = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({
type: 'INCREASE_BY',
payload: 15,
Expand Down
@@ -1,15 +1,16 @@
import type { MouseEvent } from 'react'
import Link from 'next/link'
import { useCount, useDispatchCount } from '../components/Counter'

const IndexPage = () => {
const count = useCount()
const dispatch = useDispatchCount()

const handleIncrease = (event) =>
const handleIncrease = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({
type: 'INCREASE',
})
const handleDecrease = (event) =>
const handleDecrease = (event: MouseEvent<HTMLButtonElement>) =>
dispatch({
type: 'DECREASE',
})
Expand Down
20 changes: 20 additions & 0 deletions examples/with-context-api/tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"moduleResolution": "node",
"module": "esnext",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
13 changes: 13 additions & 0 deletions packages/next/build/analysis/get-page-static-info.ts
Expand Up @@ -53,6 +53,19 @@ export function checkExports(swcAST: any): { ssr: boolean; ssg: boolean } {
}
}

if (
node.type === 'ExportDeclaration' &&
node.declaration?.type === 'VariableDeclaration'
) {
const id = node.declaration?.declarations[0]?.id.value
if (['getStaticProps', 'getServerSideProps'].includes(id)) {
return {
ssg: id === 'getStaticProps',
ssr: id === 'getServerSideProps',
}
}
}

if (node.type === 'ExportNamedDeclaration') {
const values = node.specifiers.map(
(specifier: any) =>
Expand Down
12 changes: 12 additions & 0 deletions test/unit/fixtures/page-runtime/ssr-variable-gssp.js
@@ -0,0 +1,12 @@
export default function Nodejs() {
return 'nodejs'
}

// export an identifier instead of function
export const getServerSideProps = async () => {
return { props: {} }
}

export const config = {
runtime: 'experimental-edge',
}
Expand Up @@ -9,21 +9,25 @@ function createNextConfig(runtime?: 'experimental-edge' | 'nodejs') {
}
}

describe('parse page runtime config', () => {
describe('parse page static info', () => {
it('should parse nodejs runtime correctly', async () => {
const { runtime } = await getPageStaticInfo({
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/nodejs-ssr.js'),
nextConfig: createNextConfig(),
})
expect(runtime).toBe('nodejs')
expect(ssr).toBe(true)
expect(ssg).toBe(false)
})

it('should parse static runtime correctly', async () => {
const { runtime } = await getPageStaticInfo({
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/nodejs.js'),
nextConfig: createNextConfig(),
})
expect(runtime).toBe(undefined)
expect(ssr).toBe(false)
expect(ssg).toBe(false)
})

it('should parse edge runtime correctly', async () => {
Expand All @@ -41,22 +45,35 @@ describe('parse page runtime config', () => {
})
expect(runtime).toBe(undefined)
})

it('should parse ssr info with variable exported gSSP correctly', async () => {
const { ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/ssr-variable-gssp.js'),
nextConfig: createNextConfig(),
})
expect(ssr).toBe(true)
expect(ssg).toBe(false)
})
})

describe('fallback to the global runtime configuration', () => {
it('should fallback when gSP is defined and exported', async () => {
const { runtime } = await getPageStaticInfo({
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/fallback-with-gsp.js'),
nextConfig: createNextConfig('experimental-edge'),
})
expect(runtime).toBe('experimental-edge')
expect(ssr).toBe(false)
expect(ssg).toBe(true)
})

it('should fallback when gSP is re-exported from other module', async () => {
const { runtime } = await getPageStaticInfo({
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'),
nextConfig: createNextConfig('experimental-edge'),
})
expect(runtime).toBe('experimental-edge')
expect(ssr).toBe(false)
expect(ssg).toBe(true)
})
})

0 comments on commit 54e7dc5

Please sign in to comment.