Skip to content

Commit 31b32ef

Browse files
authored
feat: deprecates getPayloadHMR in favor of simpler getPayload (#9249)
Deprecates `getPayloadHMR` and simplifies this pattern into a single `import { getPayload } from 'payload'`. We will still retain the exported `getPayloadHMR` but it now will throw a deprecation warning with instructions for how to migrate.
1 parent 67ff23a commit 31b32ef

File tree

58 files changed

+263
-303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+263
-303
lines changed

docs/getting-started/concepts.mdx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,10 @@ Here's a quick example of a React Server Component fetching data using the Local
6868
```tsx
6969
import React from 'react'
7070
import config from '@payload-config'
71-
import { getPayloadHMR } from '@payloadcms/next/utilities'
71+
import { getPayload } from 'payload'
7272

7373
const MyServerComponent: React.FC = () => {
74-
// If you're working in Next.js, and you want HMR,
75-
// you should get Payload via the `getPayloadHMR` function.
76-
const payload = await getPayloadHMR({ config })
77-
78-
// If you are writing a standalone script and do not need HMR,
79-
// you can get Payload via import { getPayload } from 'payload' instead.
74+
const payload = await getPayload({ config })
8075

8176
// The `findResult` here will be fully typed as `PaginatedDocs<Page>`,
8277
// where you will have the `docs` that are returned as well as

docs/live-preview/server.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ Then, render the `RefreshRouteOnSave` component anywhere in your `page.tsx`. Her
3434

3535
```tsx
3636
import { RefreshRouteOnSave } from './RefreshRouteOnSave.tsx'
37-
import { getPayloadHMR } from '@payloadcms/next/utilities'
37+
import { getPayload } from 'payload'
3838
import config from '../payload.config'
3939

4040
export default async function Page() {
41-
const payload = await getPayloadHMR({ config })
41+
const payload = await getPayload({ config })
4242

4343
const page = await payload.findByID({
4444
collection: 'pages',

docs/local-api/outside-nextjs.mdx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ Payload can be used completely outside of Next.js which is helpful in cases like
1818

1919
Payload provides a convenient way to run standalone scripts, which can be useful for tasks like seeding your database or performing one-off operations.
2020

21-
In standalone scripts, can simply import the Payload Config and use it right away. If you need an initialized copy of Payload, you can then use the `getPayload` function. This can be useful for tasks like seeding your database or performing other one-off operations.
21+
In standalone scripts, you can simply import the Payload Config and use it right away. If you need an initialized copy of Payload, you can then use the `getPayload` function. This can be useful for tasks like seeding your database or performing other one-off operations.
2222

2323
```ts
24-
// We are importing `getPayload` because we don't need HMR
25-
// for a standalone script. For usage of Payload inside Next.js,
26-
// you should always use `import { getPayloadHMR } from '@payloadcms/next/utilities'` instead.
2724
import { getPayload } from 'payload'
2825
import config from '@payload-config'
2926

docs/local-api/overview.mdx

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,18 @@ const afterChangeHook: CollectionAfterChangeHook = async ({ req: { payload } })
4343

4444
If you want to import Payload in places where you don't have the option to access it from function arguments or `req`, you can import it and initialize it.
4545

46-
There are two places to import Payload:
47-
48-
**Option 1 - using HMR, within Next.js**
49-
5046
```ts
51-
import { getPayloadHMR } from '@payloadcms/next/utilities'
47+
import { getPayload } from 'payload'
5248
import config from '@payload-config'
5349

54-
const payload = await getPayloadHMR({ config })
50+
const payload = await getPayload({ config })
5551
```
5652

57-
You should import Payload using the first option (`getPayloadHMR`) if you are using Payload inside of Next.js (like route handlers, server components, and similar.)
58-
59-
This way, in Next.js development mode, Payload will work with Hot Module Replacement (HMR), and as you make changes to your Payload Config, your usage of Payload will always be in sync with your changes. In production, `getPayloadHMR` simply disables all HMR functionality so you don't need to write your code any differently. We handle optimization for you in production mode.
53+
If you're working in Next.js' development mode, Payload will work with Hot Module Replacement (HMR), and as you make changes to your Payload Config, your usage of Payload will always be in sync with your changes. In production, `getPayload` simply disables all HMR functionality so you don't need to write your code any differently. We handle optimization for you in production mode.
6054

6155
If you are accessing Payload via function arguments or `req.payload`, HMR is automatically supported if you are using it within Next.js.
6256

63-
**Option 2 - outside of Next.js**
64-
65-
If you are using Payload outside of Next.js, for example in standalone scripts or in other frameworks, you can import Payload with no HMR functionality. Instead of using `getPayloadHMR`, you can use `getPayload`.
66-
67-
```ts
68-
import { getPayload } from 'payload'
69-
import config from '@payload-config'
70-
71-
const payload = await getPayload({ config })
72-
```
73-
74-
Both options function in exactly the same way outside of one having HMR support and the other not. For more information about using Payload outside of Next.js, [click here](./outside-nextjs).
57+
For more information about using Payload outside of Next.js, [click here](./outside-nextjs).
7558

7659
## Local options available
7760

examples/auth/payload/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ See the [Collections](https://payloadcms.com/docs/configuration/collections) doc
4848

4949
```ts
5050
import { headers as getHeaders } from 'next/headers.js'
51-
import { getPayloadHMR } from '@payloadcms/next/utilities'
51+
import { getPayload } from 'payload'
5252
import config from '../../payload.config'
5353

5454
export default async function AccountPage({ searchParams }) {
5555
const headers = getHeaders()
56-
const payload = await getPayloadHMR({ config: configPromise })
56+
const payload = await getPayload({ config: configPromise })
5757
const { permissions, user } = await payload.auth({ headers })
5858

5959
if (!user) {

examples/auth/payload/src/app/(app)/account/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { getPayloadHMR } from '@payloadcms/next/utilities'
21
import { headers as getHeaders } from 'next/headers.js'
32
import Link from 'next/link'
43
import { redirect } from 'next/navigation'
4+
import { getPayload } from 'payload'
55
import React, { Fragment } from 'react'
66

77
import config from '../../../payload.config'
@@ -14,7 +14,7 @@ import classes from './index.module.scss'
1414

1515
export default async function Account() {
1616
const headers = getHeaders()
17-
const payload = await getPayloadHMR({ config })
17+
const payload = await getPayload({ config })
1818
const { permissions, user } = await payload.auth({ headers })
1919

2020
if (!user) {

examples/auth/payload/src/app/(app)/create-account/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { getPayloadHMR } from '@payloadcms/next/utilities'
21
import { headers as getHeaders } from 'next/headers.js'
32
import { redirect } from 'next/navigation'
3+
import { getPayload } from 'payload'
44
import React from 'react'
55

66
import config from '../../../payload.config'
@@ -11,7 +11,7 @@ import classes from './index.module.scss'
1111

1212
export default async function CreateAccount() {
1313
const headers = getHeaders()
14-
const payload = await getPayloadHMR({ config })
14+
const payload = await getPayload({ config })
1515
const { user } = await payload.auth({ headers })
1616

1717
if (user) {

examples/auth/payload/src/app/(app)/login/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { getPayloadHMR } from '@payloadcms/next/utilities'
21
import { headers as getHeaders } from 'next/headers.js'
32
import { redirect } from 'next/navigation'
3+
import { getPayload } from 'payload'
44
import React from 'react'
55

66
import config from '../../../payload.config'
77
import { Gutter } from '../_components/Gutter'
88
import { RenderParams } from '../_components/RenderParams'
9-
import { LoginForm } from './LoginForm'
109
import classes from './index.module.scss'
10+
import { LoginForm } from './LoginForm'
1111

1212
export default async function Login() {
1313
const headers = getHeaders()
14-
const payload = await getPayloadHMR({ config })
14+
const payload = await getPayload({ config })
1515
const { user } = await payload.auth({ headers })
1616

1717
if (user) {

examples/auth/payload/src/app/(app)/logout/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { getPayloadHMR } from '@payloadcms/next/utilities'
21
import { headers as getHeaders } from 'next/headers.js'
32
import Link from 'next/link'
3+
import { getPayload } from 'payload'
44
import React from 'react'
55

66
import config from '../../../payload.config'
77
import { Gutter } from '../_components/Gutter'
8-
import { LogoutPage } from './LogoutPage'
98
import classes from './index.module.scss'
9+
import { LogoutPage } from './LogoutPage'
1010

1111
export default async function Logout() {
1212
const headers = getHeaders()
13-
const payload = await getPayloadHMR({ config })
13+
const payload = await getPayload({ config })
1414
const { user } = await payload.auth({ headers })
1515

1616
if (!user) {

examples/auth/payload/src/app/(app)/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { getPayloadHMR } from '@payloadcms/next/utilities'
21
import { headers as getHeaders } from 'next/headers.js'
32
import Link from 'next/link'
3+
import { getPayload } from 'payload'
44
import React, { Fragment } from 'react'
55

66
import config from '../../payload.config'
@@ -9,7 +9,7 @@ import { HydrateClientUser } from './_components/HydrateClientUser'
99

1010
export default async function HomePage() {
1111
const headers = getHeaders()
12-
const payload = await getPayloadHMR({ config })
12+
const payload = await getPayload({ config })
1313
const { permissions, user } = await payload.auth({ headers })
1414

1515
return (

0 commit comments

Comments
 (0)