Enables setting base-url to / for relative URLs in serve mode (#1853)

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 `/`.
This commit is contained in:
Colin Dean 2022-05-10 16:04:40 -04:00 committed by GitHub
parent beb93f2b0a
commit f7f5545bdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View file

@ -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.
@ -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
```

View file

@ -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();