question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

More Tooling for Static Analysis of Pages

See original GitHub issue

Motivation

I’m building a blog, and I want to be able to show a listing page that links to all pages within a posts directory.

Problem

Since posts are just pages, I can theoretically generate a list of page-metadata statically at build-time–I’ve tested this with import.meta.glob, and it works mostly fine, but I have to manually construct links from the filenames, which is brittle since it’s bypassing any custom routing functionality I may create in the future:

async function getPosts(): Promise<PostMetadata[]> {
  const files = import.meta.globEager('/pages/posts/**/*.page.*([a-zA-Z0-9])');
  return map(files, (file, path) => {
    const { title, publishedAt } = file;
    return {
      publishedAt,
      title,
      url: path.replace('index.page.tsx', '').replace('pages/', ''),
    };
  });
}

Obviously I could manually create this list of URLs, but I’ve found even this hacky solution to be extremely nice from a developer-experience perspective, and would like to see some tooling to support this further.

Proposed solution

Expose an API for accessing routing details for all pre-rendered pages. It seems there may potentially be some overlap with https://github.com/brillout/vite-plugin-ssr/issues/49 here. I’m actually not sure if this is potentially an intractable problem, since this would require knowledge of all pages that will be prerendered (and what their routes would look like) before prerender hooks are called.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
brilloutcommented, Dec 13, 2021

I agree there should be an API for that.

In the meantime, have you seen pageContext._pageRoutes? You still have to manually determine the URLs but it will at least provide you with a consistent list of page IDs and their routes.

But a non-internal and higher-level API should be made available to users. E.g.

type PageInstance = {
  url: string,
  pageContext: {
    Page: ReactComponent | VueComponent | unknown
    pageExports: Record<string, unknown>
    // ...
  }
}

const pageInstances: PageInstance[] =
  // `partial: true`: disable `vite-plugin-ssr` warning upon dynamic routes (which cannot be resolved
  //                  without the user providing a list of URLs).
  // `loadPageContext: true` => loads all `.page.js` files
  // `loadPageContext: false` => only loads the `.page.route.js` files
  await pageContext.getPageInstances({ partial: true, loadPageContext: true })
0reactions
brilloutcommented, Apr 19, 2022

In general, eagerly loading pages is an anti-pattern for performance & scalability; it goes against Vite’s lazy-transpiling approach.

An alternative is to use https://github.com/antfu/vite-plugin-glob with a custom transformer (see https://github.com/antfu/vite-plugin-glob#custom-queries). The transformer would extract the meta data in a performant way. That way we can get meta data for all pages and stay performant & scalable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TOP 40 Static Code Analysis Tools (Best Source Code ...
RIPS is the only code analysis solution that performs language-specific security analysis. It detects the most complex security vulnerabilities ...
Read more >
41 Static Analysis Tools To Help You Analyze Source Code
15 static analysis tools. Though programmers can perform static analysis manually, it's more common to use automated tools for this testing.
Read more >
Static analysis in JavaScript: 11 tools to help you catch errors ...
In this guide, we'll look at some of the most prominent static analysis tools available in the JavaScript ecosystem and discuss why and...
Read more >
List of tools for static code analysis - Wikipedia
This is a list of notable tools for static program analysis Contents. 1 Static code analysis tools; 2 Languages. 2.1 Ada; 2.2 C,...
Read more >
️ A curated list of static analysis (SAST) tools for all ... - GitHub
A curated list of static analysis (SAST) tools for all programming languages, config files, build tools, and more. The focus is on tools...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found