Show suggested pages on 404 page #3481
delucis
started this conversation in
Feature Requests
Replies: 1 comment
-
|
Some considerations:
I tested this idea out when it was first suggested to see how it might work. It is possible with relatively little code for a basic PoC: <page-suggestions>
<p>Loading suggested pages…</p>
</page-suggestions>
<script type="module">
const pagefind = await import("/pagefind/pagefind.js");
pagefind.init();
const { pathname } = window.location;
const query = pathname
.replaceAll(/[^A-z0-9]/g, ' ') // way too simple, would wreck non-Latin languages
.replaceAll(/\s+/g, ' ')
.trim();
const search = await pagefind.search(query);
const results = await Promise.all(search.results.slice(0, 3).map((result) => result.data()));
class Suggestions extends HTMLElement {
connectedCallback() {
const p = this.querySelector('p');
const list = document.createElement('ul');
for (const result of results) {
const li = document.createElement('li');
const link = document.createElement('a');
link.href = result.url;
link.textContent = result.meta.title;
li.append(link);
list.append(li);
}
p.textContent = 'Maybe you were looking for one of these pages?';
this.append(list);
console.log(results);
}
}
customElements.define('page-suggestions', Suggestions);
</script>(The |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
@saif-shines suggested this a few weeks ago, so I’m capturing it in a feature request before we forget.
What version of
starlightare you using?0.36.1
What is your idea?
The basic idea is to try to suggest pages a user may be interested in based on the 404 URL.
For example, if a user lands on
/internationaliztion-guide, search for"internationaliztion guide"and render the results to the 404 page.Why is this feature necessary?
This may help a user find what they’re looking for more quickly when faced with a 404. Instead of our default suggestion to “Check the URL or try using the search bar.” we would provide some immediate options for them to pick from.
Do you have examples of this feature in other projects?
I’m sure I’ve seen this before, but can’t point to anything off the top of my head.
Participation
Beta Was this translation helpful? Give feedback.
All reactions