observablehq / framework

A static site generator for data apps, dashboards, reports, and more. Observable Framework combines JavaScript on the front-end for interactive graphics with any language on the back-end for data analysis.
https://observablehq.com/framework/
ISC License
2.58k stars 125 forks source link

Dynamically generated links with absolute paths don't work after build #1677

Closed mcmcclur closed 2 months ago

mcmcclur commented 2 months ago

If you dynamically generate links from one page within a Framework project to another page within the same project using code, you might find that it works in development mode but not in production.

This basic difficulty is that the bundler modifies absolute paths for links within a project but fails to do so in some complicated situations. Thus one link looks like

observablehq.cloud/project-name/page

while the other looks only like

observablehq.cloud/page

I'm not sure if I'd definitely call this a bug but it certainly confused me for a bit.


Here's a concrete example on observablehq.cloud and here's the source code.

Fil commented 2 months ago

The root cause is that the development site is hosted under /, and the deployed site is hosted under /project-name/. Framework only converts links when it can statically analyze them. I can see how this might be confusing in some cases such as the one you're describing; in this case the only way to solve the difference is to use a relative link (e.g., ../../posts/RevealJS/).