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): sort numbered middleware/plugins from all layers #25906

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

markthree
Copy link

@markthree markthree commented Feb 22, 2024

πŸ”— Linked issue

#25891

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Only sort the global middleware, if there are numbers, they will be moved forward in order, Resolves #25891

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have added tests (if possible).
  • I have updated the documentation accordingly.

Copy link

stackblitz bot commented Feb 22, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Co-authored-by: Damian GΕ‚owala <damian.glowala.rebkow@gmail.com>
@DamianGlowala DamianGlowala changed the title fix(nuxt): sort global middleware on layers, close #25891 fix(nuxt): sort global middleware in layers Feb 22, 2024
Copy link
Member

@danielroe danielroe 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! ❀️

We create the middleware here:

app.middleware = []
for (const config of reversedConfigs) {
const middlewareDir = (config.rootDir === nuxt.options.rootDir ? nuxt.options : config).dir?.middleware || 'middleware'
const middlewareFiles = await resolveFiles(config.srcDir, `${middlewareDir}/*{${nuxt.options.extensions.join(',')}}`)
for (const file of middlewareFiles) {
const name = getNameFromPath(file)
if (!name) {
// Ignore files like `~/middleware/index.vue` which end up not having a name at all
logger.warn(`No middleware name could be resolved for \`~/${relative(nuxt.options.srcDir, file)}\`. Bear in mind that \`index\` is ignored for the purpose of creating a middleware name.`)
continue
}
app.middleware.push({ name, path: file, global: hasSuffix(file, '.global') })
}
}

We could sort them there, but I think it would (also?) be better to sort the results coming from resolveFiles:

export async function resolveFiles (path: string, pattern: string | string[], opts: { followSymbolicLinks?: boolean } = {}) {
const files = await globby(pattern, { cwd: path, followSymbolicLinks: opts.followSymbolicLinks ?? true })
return files.map(p => resolve(path, p)).filter(p => !isIgnored(p)).sort()
}

It would also be amazing if you could add a test case here:

it('resolves layer middleware in correct order', async () => {
const app = await getResolvedApp([
// layer 1
'layer1/middleware/global.global.ts',
'layer1/middleware/named-from-layer.ts',
'layer1/middleware/named-override.ts',
'layer1/nuxt.config.ts',
// layer 2
'layer2/middleware/global.global.ts',
'layer2/middleware/named-from-layer.ts',
'layer2/middleware/named-override.ts',
'layer2/plugins/override-test.ts',
'layer2/nuxt.config.ts',
// final (user) layer
'middleware/named-override.ts',
'middleware/named.ts',
{
name: 'nuxt.config.ts',
contents: 'export default defineNuxtConfig({ extends: [\'./layer2\', \'./layer1\'] })'
}
])
const fixtureMiddleware = app.middleware.filter(p => p.path.includes('<rootDir>')).map(p => p.path)
// TODO: fix this
expect(fixtureMiddleware).toMatchInlineSnapshot(`
[
"<rootDir>/layer2/middleware/global.global.ts",
"<rootDir>/layer2/middleware/named-from-layer.ts",
"<rootDir>/middleware/named-override.ts",
"<rootDir>/middleware/named.ts",
]
`)
})

@markthree
Copy link
Author

@danielroe Thanks. I'll try it.

@markthree markthree changed the title fix(nuxt): sort global middleware in layers fix(nuxt): resolves layer middleware in correct order Feb 22, 2024
@markthree
Copy link
Author

@danielroe,It's done, and while this way of writing it isn't the best for performance, it's clear enough and also introduces as little break change as possible. πŸ₯°

Maybe in the future we can have resolveFiles upper level tools that can handle it, but I can't think of a good design right now (without introducing break change) 🀣

@DamianGlowala DamianGlowala changed the title fix(nuxt): resolves layer middleware in correct order fix(nuxt): resolve layer middleware in correct order Feb 22, 2024
@danielroe danielroe changed the title fix(nuxt): resolve layer middleware in correct order fix(nuxt): sort numbered middleware by number Mar 15, 2024
@danielroe danielroe changed the title fix(nuxt): sort numbered middleware by number fix(nuxt): sort numbered middleware Mar 15, 2024
@danielroe
Copy link
Member

danielroe commented Mar 15, 2024

I'm rethinking this. Plugins explicitly don't behave this way:

expect(fixturePlugins).toMatchInlineSnapshot(`
[
"<rootDir>/layer1/plugins/02.plugin.ts",
"<rootDir>/layer1/plugins/object-named.ts",
"<rootDir>/layer1/plugins/override-test.ts",
"<rootDir>/layer2/plugins/01.plugin.ts",
"<rootDir>/layer2/plugins/object-named.ts",
"<rootDir>/layer2/plugins/override-test.ts",
"<rootDir>/plugins/00.plugin.ts",
"<rootDir>/plugins/object-named.ts",
]

I think if we want numbered plugins/middleware to be sorted and hoisted out, they should have the same behaviour.

Would you be able to update this PR so both are handled the same? (Check #22889 for context.)

@danielroe danielroe marked this pull request as draft March 15, 2024 16:33
@danielroe danielroe marked this pull request as ready for review March 15, 2024 21:21
@danielroe danielroe changed the title fix(nuxt): sort numbered middleware feat(nuxt): sort numbered middleware/plugins from all layers Mar 15, 2024
@markthree
Copy link
Author

markthree commented Mar 16, 2024

Thank you @danielroe , I've been so busy lately, I meant to take a look at it when I had some free time. 😭

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.

Global middlewares ignore alphabetical order when using Nuxt Layers
3 participants