Skip to content

Commit

Permalink
Enables setting base-url to / for relative URLs in serve mode (#1853)
Browse files Browse the repository at this point in the history
Having to change the base URL to whatever ngrok URL was provisioned for
me was frustrating. This patch enables setting it to `/`, which will
then make the `get_url()` function simply return `/`.
  • Loading branch information
colindean authored May 10, 2022
1 parent beb93f2 commit f7f5545
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/content/documentation/getting-started/cli-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ You can also specify different addresses for the interface and base_url using `-

> By default, devices from the local network **won't** be able to access the served pages. This may be of importance when you want to test page interaction and layout on your mobile device or tablet. If you set the interface to `0.0.0.0` however, devices from your local network will be able to access the served pages by requesting the local ip-address of the machine serving the pages and port used.
>
> In order to have everything work correctly, you might also have to alter the `base-url` flag to your local ip.
> In order to have everything work correctly, you might also have to alter the `base-url` flag to your local ip or set it to `/` to use server-base relative paths.
Use the `--open` flag to automatically open the locally hosted instance in your
web browser.
Expand All @@ -87,6 +87,7 @@ $ zola serve --port 2000
$ zola serve --interface 0.0.0.0
$ zola serve --interface 0.0.0.0 --port 2000
$ zola serve --interface 0.0.0.0 --base-url 127.0.0.1
$ zola serve --interface 0.0.0.0 --base-url /
$ zola serve --interface 0.0.0.0 --port 2000 --output-dir www/public
$ zola serve --open
```
Expand Down
14 changes: 9 additions & 5 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,18 @@ fn create_new_site(
SITE_CONTENT.write().unwrap().clear();

let mut site = Site::new(root_dir, config_file)?;

let base_address = format!("{}:{}", base_url, interface_port);
let address = format!("{}:{}", interface, interface_port);

let base_url = if site.config.base_url.ends_with('/') {
format!("http://{}/", base_address)
let base_url = if base_url == "/" {
String::from("/")
} else {
format!("http://{}", base_address)
let base_address = format!("{}:{}", base_url, interface_port);

if site.config.base_url.ends_with('/') {
format!("http://{}/", base_address)
} else {
format!("http://{}", base_address)
}
};

site.enable_serve_mode();
Expand Down

0 comments on commit f7f5545

Please sign in to comment.