eleventy-dev-server icon indicating copy to clipboard operation
eleventy-dev-server copied to clipboard

Add option to inject chosen URL into env variable

Open nielsbom opened this issue 1 year ago • 3 comments

This PR introduces a new option to make the eleventy-dev-server insert an environment variable called ELEVENTY_SERVER_BASEURL with the full base URL that the site is served on.

The main reason for this option is that eleventy-dev-server chooses a port dynamically and I wanted to use that base URL to generate absolute URLs when developing.

Default value is false.

I had to jump through a few hoops to get the tests working because AVA is concurrent by default and shares environment variables.

nielsbom avatar Feb 14 '24 13:02 nielsbom

I wonder if there is a way to expose this to https://www.11ty.dev/docs/events/#eleventy.after?

uncenter avatar Feb 14 '24 13:02 uncenter

Something that I discovered later was that an Eleventy site first builds and then eleventy-dev-server is started so the first build does not have access to this environment variable.

nielsbom avatar Feb 14 '24 13:02 nielsbom

I wonder if there is a way to expose this to https://www.11ty.dev/docs/events/#eleventy.after?

Definitely. You can read process.env.ELEVENTY_SERVER_BASEURL and then do with that whatever you need. But the first time eleventy.after is called the environment variable will not exist because by that eleventy-dev-server has not started yet (and has not decided on a port).

nielsbom avatar Feb 14 '24 13:02 nielsbom

I think I’m okay with this one and am happy to resolve the conflicts but is this something you still want/need @nielsbom? It sounds like it doesn’t solve your problem, in the end 😅

zachleat avatar Jun 04 '24 14:06 zachleat

Is this something you still want/need @nielsbom? It sounds like it doesn’t solve your problem, in the end 😅

You're right, it didn't. Closing the PR.

nielsbom avatar Jun 04 '24 16:06 nielsbom