payload icon indicating copy to clipboard operation
payload copied to clipboard

Live preview doesn't work with relative path

Open ericuldall opened this issue 1 year ago • 1 comments

Link to reproduction

No response

Describe the Bug

I have my live preview configured as follows:

 url: '/admin/preview-screen',

For some reason I get a blank screen on the preview, but I can confirm the path it's loading is localhost:3000/admin/preview-screen

If I switch the config to:

 url: 'http://localhost:3000/admin/preview-screen',

It works...

Network tab shows a 200 on the page either way, but not getting any preview on relative implementation. I'd like to use a relative path so that the url will match wherever the endpoint is deployed and I don't have to track different configs.

To Reproduce

Set preview endpoint to a relative path on the server

Payload Version

2.0.5

Adapters and Plugins

No response

ericuldall avatar Dec 23 '23 18:12 ericuldall

A common issue I've run in to when working with NextJS is that relative paths only work client-side. For server-side routing, the fully qualified path is required. With payload moving to NextJS, it may be best practice (and included in the documentation) to set an ENV variable for the host and use something like ${process.env.PAYLOAD_PUBLIC_HOST}/admin/preview-screen. This way you can change your host for each dev/prod environment without changing your code.

revnelson avatar Dec 28 '23 16:12 revnelson

Hey @ericuldall - @revnelson's comment about using an ENV variable is the approach we use and recommend. What do you think?

PatrikKozak avatar Jan 02 '24 21:01 PatrikKozak

@ericuldall Here is an example of how using an ENV would work with a live-preview configuration:

livePreview: {
    url: ({ data }) => `${process.env.PAYLOAD_PUBLIC_SERVER_URL}/posts/${data?.slug}`,
},

This is the approach we recommend. Going to close this for now, if you have any other questions - let me know!

PatrikKozak avatar Jan 04 '24 19:01 PatrikKozak

That env var is undefined for me. Using vite bundler is the issue maybe?

ericuldall avatar Jan 04 '24 19:01 ericuldall

ah I guess I have to define the env var, I've seen lots of examples of people calling that but never mentioned it's not built in.

ericuldall avatar Jan 04 '24 19:01 ericuldall

@ericuldall Yup you need to define your env variables in a .env file

PatrikKozak avatar Jan 04 '24 19:01 PatrikKozak