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

feat(warn): warn defineAsyncComponent usage in routes #682

Merged
merged 6 commits into from
Jan 9, 2021
Merged

feat(warn): warn defineAsyncComponent usage in routes #682

merged 6 commits into from
Jan 9, 2021

Conversation

leemove
Copy link
Contributor

@leemove leemove commented Dec 29, 2020

Close #627

__tests__/lazyLoading.spec.ts Outdated Show resolved Hide resolved
src/navigationGuards.ts Outdated Show resolved Hide resolved
src/navigationGuards.ts Outdated Show resolved Hide resolved
@@ -294,6 +301,10 @@ export function extractComponentsGuards(
const resolvedComponent = isESModule(resolved)
? resolved.default
: resolved
// __asyncLoader is added by defineAsyncCompoent
if (__DEV__ && '__asyncLoader' in resolvedComponent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? What does it change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@posva
Async component can be used inside dynamic imports route components.
This will mark the async component is used inside dynamic imports route component or not.
I don't know is it the right way, or do like

    if (__DEV__ && '__asyncLoader' in resolvedComponent) {
      resolvedComponent.__lazyResolved = true;
    }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this accounting for () => defineAsyncComponent(...)? If it is, it should warn instead and it should also have a test

Copy link
Contributor Author

@leemove leemove Jan 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also accounting for any function return Promise<AsyncComponent>

Is this necessary?

and if component not return Promise. it will warn like this first at line 247.

Component "default" in record with path "/foo" is a function that does not return a Promise. If you were passing a functional component, make sure to add a "displayName" to the component. This will break in production if not fixed.

@posva

src/navigationGuards.ts Outdated Show resolved Hide resolved
@posva posva changed the title feat(warn): warn defineAsyncComponent usage in routes (#627) feat(warn): warn defineAsyncComponent usage in routes Dec 29, 2020
@leemove leemove requested a review from posva December 29, 2020 10:52
Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just kept the same unresolved points!

Copy link
Contributor Author

@leemove leemove left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just kept the same unresolved points!

Thank you for reviewing !

Those unresolved potints are all about the lazy load async component.
It's hard to distinguish between async component and dynamic imports async component after cached.
Because the the following code cache async component.

  componentPromise.then(resolved => {
      const resolvedComponent = isESModule(resolved)
        ? resolved.default
        : resolved
      // replace the function with the resolved component
      record.components[name] = resolvedComponent
  }

compont will be async componet instead of Promise<async component> when navigate to this route next time .

So I marked the resolved async componet.

  if (__DEV__ && '__asyncLoader' in resolvedComponent) {
      resolvedComponent.__resolvedAsyncComponent = true
  }

and check the mark when async componet.

 else if (
      (rawComponent as any).__asyncLoader &&
      !(rawComponent as any).__resolvedAsyncComponent // check  dynamic imports
    ) {
     // make it component promise ,  it will marked by code above, and warn only once when navigated first time.
      let asyncComponent = rawComponent
      rawComponent = () => Promise.resolve(asyncComponent)
      warn(.... )
    }
}

the next time navigate to this component, we can know it is dynamic imports or not.

Sorry about my poor English. hope you understand what I said.🙏

@@ -294,6 +301,10 @@ export function extractComponentsGuards(
const resolvedComponent = isESModule(resolved)
? resolved.default
: resolved
// __asyncLoader is added by defineAsyncCompoent
if (__DEV__ && '__asyncLoader' in resolvedComponent) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@posva
Async component can be used inside dynamic imports route components.
This will mark the async component is used inside dynamic imports route component or not.
I don't know is it the right way, or do like

    if (__DEV__ && '__asyncLoader' in resolvedComponent) {
      resolvedComponent.__lazyResolved = true;
    }

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think I understand what you said. I realized I wrote one of my comments at the wrong line. Let me know if the review isn't clear

src/navigationGuards.ts Outdated Show resolved Hide resolved
@@ -294,6 +301,10 @@ export function extractComponentsGuards(
const resolvedComponent = isESModule(resolved)
? resolved.default
: resolved
// __asyncLoader is added by defineAsyncCompoent
if (__DEV__ && '__asyncLoader' in resolvedComponent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this accounting for () => defineAsyncComponent(...)? If it is, it should warn instead and it should also have a test

@leemove
Copy link
Contributor Author

leemove commented Jan 8, 2021

I remove the accounting for () => defineAsyncComponent(...) .

because it will warn function that does not return a Promise

and remove all warn for function return Promise,like () => Promise<definAsyncComponent(...)>

it will warn when navigate to this component next time .Beacuse next time,the component here is just AsyncComponent not Promise<AsyncComponent>

@leemove leemove requested a review from posva January 8, 2021 03:36
Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I couldn't push some final changes (you should check the allow edits by maintainers) so I will do that locally

@posva posva merged commit 9520d66 into vuejs:master Jan 9, 2021
@leemove
Copy link
Contributor Author

leemove commented Jan 9, 2021

Thank you for your patient review! I'm really excited about this PR was merged!

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

Successfully merging this pull request may close these issues.

Warn against defineAsyncComponent usage in routes
2 participants