Skip to content

Adding support for hosting with URL prefix #1357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

flowkclav
Copy link

@flowkclav flowkclav commented Apr 25, 2025

Closes #163. Adding support for hosting with URL prefix to make reverse-proxy setup on sub-path possible.

Starting silverbullet server specifying a "prefix" (or "context root") in environment variable SB_URL_PREFIX will cause the application to be hosted on the specified prefix. This allows sub-path configuration when setting up reverse proxies (e.g. https://myserver/sb/ -> http://localhost:3000/sb/ )

Summary of changes:

  • server:
    • Hono configured to serve on the prefix, if specified
    • Prefix is removed from incoming URLs in server endpoints for the application to function normally
    • Prefix is added to outgoing redirects
    • Prefix is set as path in authentication cookies
    • Injection of the prefix in the base element of index.html and auth.html
  • client:
    • Switching from "absolute" URLs (i.e. host-relative, e.g. /myresource) to page-relative URLs (e.g. myresource)
    • Using document.baseURI instead of location.origin|host when constructing URLs

NOTES:

  • Running the formatter modified a file I did not otherwise touch, I did not include it in this PR.
  • Likewise, the test I added and modified pass but another test is failing, at least on my setup (JSON utils => ./plug-api/lib/json.test.ts:4:6 fails with Actual 2023-05-02 / Expected 2023-05-03. Probably a timezone thing). I left it alone.

@flowkclav flowkclav force-pushed the 163-support-subpath branch from bed4fb6 to 0c972e6 Compare May 1, 2025 00:10
@zefhemel
Copy link
Collaborator

zefhemel commented May 1, 2025

Thanks for this. I need the time to properly tests this. I anticipate a lot of edge cases and places where being hosted at the root is implicitly assumed.

@zefhemel zefhemel self-assigned this May 1, 2025
@flowkclav
Copy link
Author

Absolutely! I am fairly new to silverbullet so it is very possible that I missed something. As if to illustrate my point, I just realised I never tested in offline mode. I just did and sure enough, the service_worker.ts will need tweaking. And unlike the rest of the client code, it doesn't have access to document.baseURI to derive the prefix. I'll try to take a closer if I have some more time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support hosting at a URL path
2 participants