Skip to content

Commit d613126

Browse files
katrinafyimre
andauthored
Update --base-url recipe docs, and add warning box (#138)
* Update --base-url recipe docs, and add warning box The goal is to add more detail where I can, and to remove information which I think is no longer correct. I think this page was originally written when --base-url behaved quite differently (before --root-dir?). I've left all of the examples even though I think they will not work. The examples are all very brief so I don't know what they're intended to show. I've left them to be safe. The statements which I've removed or changed because I think they're no longer correct are: > Even though your site isn't deployed yet, lychee can verify that the > files your links point to exist in your project. > The `--base-url` parameter works similarly to other tools you might know: > --base-url: Use it when you care about your site's final URL structure * Apply suggestion from @mre Co-authored-by: Matthias Endler <matthias@endler.dev> * address some more important review feedback --------- Co-authored-by: Matthias Endler <matthias@endler.dev>
1 parent 1875626 commit d613126

File tree

1 file changed

+45
-28
lines changed

1 file changed

+45
-28
lines changed

src/content/docs/recipes/base-url.mdx

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
---
2-
title: Testing Sites Not Served from Root with --base-url
3-
description: Understanding how to use the --base-url parameter in lychee
2+
title: Applying a Base URL with --base-url
43
---
54

65
import { Code } from "@astrojs/starlight/components";
76
export const fileName = "docs/guide.html";
87
export const fileLang = "html";
98

10-
## What Is The Base URL?
9+
## What Is the Base URL?
1110

12-
The `--base-url` parameter in lychee lets you check links for sites that aren't
13-
served from the root domain. This is useful when your site lives in a
14-
subdirectory, like `example.com/docs/`.
11+
In HTML, a base URL is used when resolving relative links; relative links will be resolved
12+
[relative to](https://developer.mozilla.org/en-US/docs/Web/API/URL_API/Resolving_relative_references)
13+
the base URL. For example, a link to `page.html` with a base URL of
14+
`https://example.com/help/` would resolve to `https://example.com/help/page.html`.
1515

16-
## Do You Need To Set A Base URL?
16+
For most published sites, the base URL of a page is just the normal URL of the page.
17+
The [`<base>`][base] HTML element can be used to override the base URL for a
18+
particular HTML page, but this is rare.
1719

18-
Most of the time, you don't! Skip `--base-url` if:
20+
[base]: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/base
1921

20-
- Your site runs at a root domain (like `example.com`)
21-
- You're only checking absolute URLs
22-
- Your links should resolve relative to their files
22+
## Do You Need To Set a Base URL?
2323

24-
You need `--base-url` when your site lives in a subdirectory.
24+
Most of the time, you don't!
25+
26+
**Skip** `--base-url` **if**:
27+
28+
- You're only checking fully-qualified URLs.
29+
- Your site runs at a root domain like `example.com`.
30+
- Your relative links in local files should resolve to other local files based on their folder structure.
31+
32+
If your use case falls into the second or third case,
33+
[--root-dir](/recipes/root-dir/) may be useful instead. Setting a root
34+
directory can help to resolve absolute links, and relative links will be
35+
resolved based on a local file's path.
36+
37+
## An Example
2538

2639
Let's look at an example:
2740

@@ -43,6 +56,13 @@ To check if these links are valid, tell lychee where the site will be hosted:
4356
lychee --base-url https://example.com/docs/ "**/*.html"
4457
```
4558

59+
:::caution[Important]
60+
lychee's `--base-url` does not consider the folder structure.
61+
This can cause problems when local files have relative links to other local files based on their filesystem structure.
62+
`--base-url` applies the same base URL to _all_ local files and may break these links.
63+
:::
64+
65+
{/*
4666
:::note[Good to know]
4767
The `--base-url` parameter works similarly to other tools you might know:
4868
@@ -53,14 +73,15 @@ The `--base-url` parameter works similarly to other tools you might know:
5373
5474
If you've used any of these, lychee's `--base-url` follows the same concept.
5575
:::
76+
*/}
5677

5778
## How Does It Work?
5879

5980
When you set `--base-url`, lychee will:
6081

61-
1. Find all links in your HTML files
62-
2. Convert relative links to full URLs
63-
3. Check if the referenced files exist in your project
82+
1. Find all links in your HTML files.
83+
2. Convert relative links to full URLs using the given base URL.
84+
3. Check if the full URLs are valid and reachable.
6485

6586
Here's what happens to different types of links:
6687

@@ -93,9 +114,11 @@ Want to verify links before deploying? lychee can check if files exist in your p
93114
lychee --base-url https://example.com/docs/ "docs/**/*.html"
94115
```
95116

117+
{/*
96118
:::note
97119
Even though your site isn't deployed yet, lychee can verify that the files your links point to exist in your project. This helps catch broken links before they go live!
98120
:::
121+
*/}
99122

100123
### GitHub Pages
101124

@@ -158,21 +181,15 @@ lychee can't automatically add the slash because both forms are valid - they jus
158181

159182
## `--base-url` vs `--root-dir`
160183

161-
These solve different problems:
162-
163-
- `--base-url` is for URLs (like `https://example.com/docs/`)
164-
- Use it when you care about your site's final URL structure
165-
- Helps validate relative links
166-
167-
- [`--root-dir`](/recipes/root-dir) is for file paths (like `./public/`)
168-
- Use it to find files on your computer
169-
- Helps validate absolute paths
184+
These solve different problems, and using both together is generally not recommended.
170185

171-
You can use both together:
186+
- `--base-url` requires a URL (like `https://example.com/docs/`), and it:
187+
- Applies to all relative links in local files.
188+
- Resolves all relative links relative to the given base URL.
172189

173-
```bash
174-
lychee --base-url https://example.com/docs/ --root-dir $(pwd)/public/ "public/**/*.html"
175-
```
190+
- [`--root-dir`](/recipes/root-dir) requires a local folder path (like `./public/`), and it:
191+
- Only applies to absolute links (links beginning with `/`) in local files.
192+
- Resolves absolute links to files within the given root directory.
176193

177194
## Troubleshooting
178195

0 commit comments

Comments
 (0)