Skip to content
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

CLI adds .html suffix to target paths in the generated index, causing 404 errors in the Pagefind UI #732

Open
corneliusroemer opened this issue Oct 27, 2024 · 1 comment

Comments

@corneliusroemer
Copy link

corneliusroemer commented Oct 27, 2024

Description:
It appears that the CLI is appending an .html suffix to all target paths when generating the index, which doesn't match the structure of my static site. This results in 404 errors when navigating through the Pagefind UI, as it tries to access paths with .html appended.

Steps to Reproduce:

  1. Create a static folder with paths that don't include .html suffixes:
    static/
    ├── index
    ├── about
    └── blog/
        ├── first-post
        └── second-post
    
  2. Run the pagefind CLI to generate the index (using a glob that doesn't require .html suffix: npx pagefind --site static --glob '**/*'
  3. Use the Pagefind UI to navigate to any of the indexed paths.

Expected Behavior:
Target paths in the generated index should match the structure of the static folder without additional .html suffixes, and the Pagefind UI should navigate correctly.

I looked for a CLI option to change this behavior but couldn't find one.

Actual Behavior:

  • All target paths in the generated index have an .html suffix appended.
  • The Pagefind UI then attempts to navigate to these .html paths, leading to 404 errors, as these pages don’t exist.

GIF illustrating the behavior:
2024-10-27 02 21 23

Interestingly, this means the pagefind CLI wouldn't work with Pagefind's own website: pagefind.app:
Google Chrome Beta 2024-10-27 02 25 51

@corneliusroemer
Copy link
Author

One workaround I found is to rewrite the URLs in processResult (generated with Claude and tested to work in my case):

new PagefindUI({
    element: '#search',
    processResult: function (result) {
        // Helper function to strip .html suffix
        const stripHtmlSuffix = (url) => {
            return url.replace(/\.html($|#)/, '$1');
        };

        // Process the main URL
        result.url = stripHtmlSuffix(result.url);

        // Process URLs in sub-results
        if (result.sub_results) {
            result.sub_results.forEach((subResult) => {
                subResult.url = stripHtmlSuffix(subResult.url);
            });
        }

        // If raw_url is present, process it too
        if (result.raw_url) {
            result.raw_url = stripHtmlSuffix(result.raw_url);
        }

        return result;
    },
});

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

No branches or pull requests

1 participant