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

[0.8.x] Custom PageNotFoundError for resolvePageComponent #259

Closed
wants to merge 1 commit into from

Conversation

RobertBoes
Copy link

Currently a generic Error is thrown in resolvePageComponent, this PR changes that to a PageNotFoundError. This is particularly useful for projects that use React and want to transition from JS to TS. With this change it would be easier to load components from different locations/paths, or different file types such as jsx and tsx. With the custom exception it's a lot easier to check if the component doesn't exist or if it's another kind of error. An example in Inertia might look something like this:

import { createInertiaApp } from '@inertiajs/react'
import { resolvePageComponent, PageNotFoundError } from 'laravel-vite-plugin/inertia-helpers';
 
createInertiaApp({
  resolve: async (name) => {
    try {
      return await resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx'))
    } catch (e) {
      if (e instanceof PageNotFoundError) {
        return await resolvePageComponent(`./Pages/${name}.tsx`, import.meta.glob('./Pages/**/*.tsx'))
      }

      throw e
    }
  }
  // ...
})

@timacdonald timacdonald marked this pull request as draft December 10, 2023 23:25
@timacdonald
Copy link
Member

timacdonald commented Dec 13, 2023

Thanks for the PR.

I think we might be able to solve this via #252, where you could pass a callback as the last parameter to determine what to do when no page is able to be found.

This would give you a first party way of doing what you are trying to do here, but without the need for the named exception / try catch.

Thoughts?

We can probably still throw a named error, though.

@timacdonald timacdonald changed the title Custom PageNotFoundError for resolvePageComponent [0.8.x] Custom PageNotFoundError for resolvePageComponent Dec 13, 2023
@timacdonald timacdonald self-assigned this Dec 13, 2023
@timacdonald
Copy link
Member

Thanks so much for this feature. I've merged the changes into this related PR: #252

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants