Skip to content

Conversation

@vkarpov15
Copy link
Collaborator

Fix #15296

Summary

Moved the browser build and browser driver into this separate GitHub repo: https://github.com/mongoosejs/mongoose-browser. This lets us delete a lot of code and remove a lot of dependencies.

Outside of removing the browser lib, there's a couple of other noteworthy changes:

  1. Changed require('node:async_hooks') to require('async_hooks') as a slight workaround for the browser lib. Webpack complains about not being able to handle node: imports.
  2. Documents now have a this.$__middleware that is set in $__setSchema that we use for executing document middleware. Previously, we relied on applyHooks() to set _middleware on the Model, which would contain a filtered copy of schema.s.hooks that just contained document middleware. However, that caused trouble for the browser lib, and caused us trouble in some other places as well. The current approach of setting the filtered document middleware on the document (or document prototype) itself when $__setSchema() is called seems more robust because that ensures that a document has a $__middleware whenever it has a schema.

Still todo:

[ ] Move browser.md docs to mongoose-browser and make browser.md link out.

Examples

@vkarpov15 vkarpov15 added this to the 9.0 milestone Apr 29, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR removes all browser‐specific builds and drivers from the core codebase and shifts browser support to the separate @mongoosejs/browser repo. It also includes adjustments such as changing the async_hooks import to remove the "node:" prefix and refactoring document middleware handling so that filtered middleware is stored directly on the document.

Reviewed Changes

Copilot reviewed 19 out of 21 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/files/sample.js Removed the browser test sample to clean up obsolete browser builds.
test/docs/lean.test.js Added a workaround (deleting $__middleware) to prevent v8Serialize() from crashing.
test/deno_mocha.js Eliminated exclusion of browser tests (as those files are now removed).
lib/schema.js Introduced a new helper (_getDocumentMiddleware) for filtering out non‑document hooks.
lib/mongoose.js Changed the async_hooks import from node:async_hooks to async_hooks.
lib/helpers/model/applyHooks.js Refactored hook extraction to use the new _getDocumentMiddleware function.
lib/document.js Updated document initialization to set $__middleware and revised updateOne hook usage.
(Others: lib/drivers/browser*, lib/browser.js, lib/browserDocument.js, lib/documentProvider.js) Removed obsolete browser-specific drivers and utilities.
docs/nextjs.md Updated documentation regarding Edge Runtime to reflect removal of the browser build.
Files not reviewed (2)
  • package.json: Language not supported
  • test/files/index.html: Language not supported
Comments suppressed due to low confidence (3)

lib/mongoose.js:42

  • Ensure that using 'async_hooks' without the 'node:' prefix is compatible with your targeted Node.js environments, as this change is critical for the browser build removal.
const { AsyncLocalStorage } = require('async_hooks');

lib/document.js:3657

  • Confirm that all documents receive the correct middleware by ensuring that $__middleware is properly initialized in $__setSchema; consider adding a unit test to verify middleware execution paths.
this.$__middleware = schema._getDocumentMiddleware();

lib/helpers/model/applyHooks.js:82

  • [nitpick] Refactoring hook extraction with _getDocumentMiddleware improves maintainability; ensure that all edge cases covered in the previous manual filtering logic are preserved in this new abstraction.
const middleware = schema._getDocumentMiddleware();

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@vkarpov15
Copy link
Collaborator Author

Also worth noting there will be some followup work to this PR necessary for #15154.

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

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

Looks good to me, though this should likely also be noted in the migration guide.

Some other questions though:

  • Do i understand correctly that the browser-build plugin is basically now just a different driver instead of having extra stuff like a browserDocument?
  • Will the new documentation be on https://mongoosejs.com/docs/ or as other things in https://plugins.mongoosejs.io/?

@vkarpov15
Copy link
Collaborator Author

  1. Yes, mongoose-browser is now structured as a separate driver with minimal support from Mongoose's core. There is slightly more than just a driver though because there is also a dist/mongoose.umd.js file that contains a compiled version of Mongoose with the driver, so you can either import Mongoose with a script tag or import Mongoose without having to compile it.
  2. For now, I plan on moving the browser docs to the mongoose-browser GitHub repo's README. Depending on demand, we may create a separate website for mongoose-browser docs, but right now we don't have any idea of how much the Mongoose browser build is used.

@vkarpov15 vkarpov15 merged commit c28738e into 9.0 May 5, 2025
29 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-15296 branch May 5, 2025 19:09
@vkarpov15 vkarpov15 mentioned this pull request Jun 18, 2025
@vkarpov15 vkarpov15 mentioned this pull request Jul 3, 2025
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.

3 participants