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: nuxt 3 support #114

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

nozomuikuta
Copy link

@nozomuikuta nozomuikuta commented Dec 14, 2022

Replace nuxt/framework#108 with minimal spec for fast upgrade.
Sorry, I had to recreate PR because I changed my personal email settings.


This draft PR aims to take over nuxt/framework#103 and resolves nuxt/framework#106.

At first, for the next major release, I triaged all the open issues as below (except Renovate Dependency Dashboard (#99)).

My implementation may include some breaking changes in API, as done in nuxt/framework#103, in a way that I believe is good and beneficial enough for users. I would like to discuss with authors and community while developing this PR.

This PR doesn't aim to add Docus-powered documentation, which is handled in nuxt/framework#103, so that we can keep this PR's "done of definition" manageable.

This PR message will be modified as needed.

TODOs

Out of Scope

References

@nozomuikuta nozomuikuta mentioned this pull request Dec 14, 2022
28 tasks
@nozomuikuta nozomuikuta marked this pull request as draft December 14, 2022 18:11

nuxt.hook('nitro:init', (nitro) => {
nitro.hooks.hook('close', async () => {
const routes = (nitro._prerenderedRoutes || []).filter(prerenderedRoute => prerenderedRoute.fileName?.endsWith('.html'))
Copy link
Author

Choose a reason for hiding this comment

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

👀 routes is an empty array so that no item is added to feeds. Need some configuration in somewhere else for it to work?

.eslintrc Outdated
Comment on lines 5 to 12
"rules": {
"no-use-before-define": ["off"],
"@typescript-eslint/no-use-before-define": ["error", {
"functions": false,
"classes": false,
"variables": false,
"typedefs": true
}],
Copy link
Author

Choose a reason for hiding this comment

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

@danielroe

IMHO, it's convenient to have "typedefs": true defined in @nuxtjs/eslint-config-typescript, because it would be common to refer to other types before their definitions.

What do you think?

(note: the other 3 flags are copied from the output of yarn eslint --print-config ./src/module.ts, so I believe they are defined in somewhere in the extended configs.)

@nozomuikuta
Copy link
Author

nozomuikuta commented Apr 3, 2023

As of v3.3.1, Nuxt supports nitro:build:public-assets hook, which is said to be the successor to generate:done hook.

I believe now it's time to complete this PR.

@danielroe

Is it simply interchangeable, or is there difference from generate:done hook on usage?

Copy link
Member

It should be interchangeable for the purpose of this module.

@nozomuikuta
Copy link
Author

Hmm, if I'm correct, this module has to know all routes inside server route(s) to return feed file(s) as response, and I can't find the way...

Current code in my local looks like this.
I checked app instance passed via app:templates, but there doesn't seem to be information about routes 🤔

Or, am I totally taking wrong approach?

    nuxt.hook('pages:extend', (pages) => {
      sources.forEach((source, index) => {
        const { dst } = addTemplate({
          filename: `feed-${source.type}-${index}.mjs`,
          getContents: () => `
import { Feed } from 'feed'
import { defineEventHandler } from 'h3'

export default defineEventHandler(async (event) => {
  const feed = new Feed()

  feed.options = {
    generator: 'https://github.com/nuxt-community/feed-module',
  }

  ${pages.forEach((page) => `
  feed.addItem({
    title: ${page.meta?.title ?? ''},
    id: ${page.path},
    link: ${page.path},
    description: ${page.meta?.description ?? ''},
    date: new Date()
  })`.trim())}

  return feed.${source.type}()
})`.trimStart(),
          write: true // debug
        })

        addServerHandler({
          route: source.path,
          handler: dst
        })
      })

@nozomuikuta
Copy link
Author

Another point about which I want to clarify is that, since Nuxt 3 modules are all what is called build modules in Nuxt 2, runtime code that they create can't refer to variables passed to module function itself. Then, there is no safe way to support this modules' create option which is a function, is it? Using toString method on the function may result in unexpected behavior.

@danielroe
Copy link
Member

You can get each generated route via the prerender:generate Nitro hook. You could use that to add them to an array/Set which you then use in nitro server route. (Don't add this route with addTemplate but use the nitro.virtual vfs as the final Nitro build will happen 'after' the prerendering step.)

You're right that we advise against including runtime code in nuxt.config. Instead, I would recommend users configure this within their app.config file. You can add the typings in the module for an object with feed key so they get type support.

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.

3 participants