Skip to content

feat(generator): export:route hook and setPayload #7422

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

Merged
merged 10 commits into from
May 27, 2020
Merged

Conversation

pi0
Copy link
Member

@pi0 pi0 commented May 27, 2020

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

This PR allows providing a shared payload to all nuxt export routes similar to generate payload

Thanks for @nilsnh (#6159 (comment)) for discovering this :)

Usage:

nuxt.config

export default {
  target: 'static',
  hooks: {
    export: {
      async before ({ setPayload }) {
        const categories = await backend.getCategories()
        setPayload({ categories })
      }
    }
  }
}

pages/index.vue

export default {
  async asyncData({ params, error, payload }) {
    if (payload) { return { categories: payload.categories } }
    return { categories: await backend.getCategories() }
  }
}

It is also possible for modules using export:route to fill payload per route.

Why using setPayload and not directly allow setting payload props?

Current usage is checking payload is not null (default value) docs. So it changes null to object on first set.

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: #)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests are passing.

Sorry, something went wrong.

@pi0 pi0 requested review from atinux and a team May 27, 2020 13:18
pooya parsa and others added 3 commits May 27, 2020 15:27

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
clarkdo
clarkdo previously approved these changes May 27, 2020

// Shared payload
this._payload = null
this.setPayload = (payload) => {
Copy link
Member

Choose a reason for hiding this comment

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

Would getter/setter for payload make more sense here ?

Copy link
Member Author

Choose a reason for hiding this comment

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

We may need something like a proxy then for deep watch. setPayload can ensure it is deep assigned and is simple :)

Copy link
Member

@clarkdo clarkdo May 27, 2020

Choose a reason for hiding this comment

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

This make me a little confused, we have this.setPayload which is never called.

We also have another setPayload as a scoped var

Copy link
Member Author

Choose a reason for hiding this comment

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

The one set in the constructor is used by export:before hook to set shared payload.

The one in scoped var is used for export:route hook.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I didn’t realize it’s passed by this.

Copy link
Member

Choose a reason for hiding this comment

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

I think I might have understood what’s happening here, so we have a global payload, each route has a scoped payload, what we’re doing is to make sure default value is always populated to updated payload.

@codecov-commenter
Copy link

codecov-commenter commented May 27, 2020

Codecov Report

Merging #7422 into dev will increase coverage by 0.05%.
The diff coverage is 84.21%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev    #7422      +/-   ##
==========================================
+ Coverage   70.20%   70.26%   +0.05%     
==========================================
  Files          88       88              
  Lines        3679     3696      +17     
  Branches     1007     1008       +1     
==========================================
+ Hits         2583     2597      +14     
- Misses        889      892       +3     
  Partials      207      207              
Flag Coverage Δ
#unittests 70.26% <84.21%> (+0.05%) ⬆️
Impacted Files Coverage Δ
packages/generator/src/generator.js 86.29% <84.21%> (-0.38%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 618eb5f...fb20767. Read the comment docs.

@pi0 pi0 merged commit a82f8d8 into dev May 27, 2020
@pi0 pi0 deleted the feat/export-route-payload branch May 27, 2020 14:51
@pi0 pi0 restored the feat/export-route-payload branch May 27, 2020 14:51
@pi0 pi0 deleted the feat/export-route-payload branch May 27, 2020 14:52
@pi0 pi0 mentioned this pull request Jun 10, 2020
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants