Skip to content

Commit feaa395

Browse files
authored
fix: support USE_HTTPS for local hmr (#14053)
Closes #12087 When using `--experimental-https` from Nextjs we have no way of detecting that right now so I've added documentation on how to handle this flag and added to support for `USE_HTTPS` to set the websocket protocol for HMR to `wss` instead of `ws`
1 parent 5a6f361 commit feaa395

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

docs/troubleshooting/troubleshooting.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,21 @@ To further investigate the issue:
126126
- Go to the login screen. Open your inspector. Go to the Network tab.
127127
- Log in and then find the login request that should appear in your network panel. Click the login request.
128128
- The login request should have a Set-Cookie header on the response, and the cookie should be getting set successfully. If it is not, most browsers generally have a little yellow ⚠️ symbol that you can hover over to see why the cookie was rejected.
129+
130+
## Using --experimental-https
131+
132+
If you are using the `--experimental-https` flag when starting your Payload server, you may run into issues with your WebSocket connection for HMR (Hot Module Reloading) in development mode.
133+
134+
To resolve this, you can set the `USE_HTTPS` environment variable to `true` in your `.env` file:
135+
136+
```
137+
USE_HTTPS=true
138+
```
139+
140+
This will ensure that the WebSocket connection uses the correct protocol (`wss://` instead of `ws://`) when HTTPS is enabled.
141+
142+
Alternatively if more of your URL is dynamic, you can set the full URL for the WebSocket connection using the `PAYLOAD_HMR_URL_OVERRIDE` environment variable:
143+
144+
```
145+
PAYLOAD_HMR_URL_OVERRIDE=wss://localhost:3000/_next/webpack-hmr
146+
```

packages/payload/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,13 +1120,14 @@ export const getPayload = async (
11201120
) {
11211121
try {
11221122
const port = process.env.PORT || '3000'
1123+
const protocol = process.env.USE_HTTPS === 'true' ? 'wss' : 'ws'
11231124

11241125
const path = '/_next/webpack-hmr'
11251126
// The __NEXT_ASSET_PREFIX env variable is set for both assetPrefix and basePath (tested in Next.js 15.1.6)
11261127
const prefix = process.env.__NEXT_ASSET_PREFIX ?? ''
11271128

11281129
cached.ws = new WebSocket(
1129-
process.env.PAYLOAD_HMR_URL_OVERRIDE ?? `ws://localhost:${port}${prefix}${path}`,
1130+
process.env.PAYLOAD_HMR_URL_OVERRIDE ?? `${protocol}://localhost:${port}${prefix}${path}`,
11301131
)
11311132

11321133
cached.ws.onmessage = (event) => {

0 commit comments

Comments
 (0)