Skip to content

Commit

Permalink
fix: do not increase version param if file is not hot reloadable
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Apr 25, 2024
1 parent ebd7468 commit 117f9c1
Show file tree
Hide file tree
Showing 5 changed files with 4,803 additions and 3,756 deletions.
8 changes: 8 additions & 0 deletions .changeset/spotty-bottles-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'hot-hook': patch
---

Bugfix : We should not invalidate non-reloadable files when a reloadable file is changed. That means, we gonna keep fetching the same file from the cache and not increase the version query param. This was not the case until this commit and caused theses issues for example :

- https://github.com/orgs/adonisjs/discussions/4544
- https://github.com/orgs/adonisjs/discussions/4537
2 changes: 2 additions & 0 deletions packages/hot_hook/src/dependency_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default class DependencyTree {

childNode.parents?.add(parentNode)
parentNode.dependencies.add(childNode)
this.addDependent(parentNode.path, childNode.path)
}

/**
Expand All @@ -116,6 +117,7 @@ export default class DependencyTree {
if (!invalidatedFiles.has(currentPath)) {
const node = this.#pathMap.get(currentPath)
if (!node) continue
if (!this.isReloadable(currentPath)) continue

node.version++
invalidatedFiles.add(currentPath)
Expand Down
1 change: 0 additions & 1 deletion packages/hot_hook/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ export class HotHookLoader {
const reloadable = context.importAttributes.hot === 'true' ? true : isHardcodedBoundary

this.#dependencyTree.addDependency(parentPath, { path: resultPath, reloadable })
this.#dependencyTree.addDependent(resultPath, parentPath)
}

if (this.#pathIgnoredMatcher.match(resultPath)) {
Expand Down
22 changes: 22 additions & 0 deletions packages/hot_hook/tests/dependency_tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,26 @@ test.group('Dependency tree', () => {
assert.deepEqual(tree.isReloadable('models/post.ts'), true)
assert.deepEqual(tree.isReloadable('services/post_service.ts'), true)
})

test('should not increase the version of a file if it is not reloadable', ({ assert }) => {
const tree = new DependencyTree({ root: 'app.ts' })

tree.addDependency('app.ts', { path: 'start/index.ts' })
tree.addDependency('start/index.ts', { path: 'config/auth.ts' })
tree.addDependency('config/auth.ts', { path: 'models/user.ts' })
tree.addDependency('start/index.ts', {
reloadable: true,
path: 'controllers/users_controller.ts',
})

tree.addDependency('controllers/users_controller.ts', { path: 'models/user.ts' })

assert.isFalse(tree.isReloadable('models/user.ts'))
assert.deepEqual(tree.getVersion('models/user.ts'), 0)

const invalidatedFiles = tree.invalidateFileAndDependents('controllers/users_controller.ts')

assert.notInclude(Array.from(invalidatedFiles), 'models/user.ts')
assert.deepEqual(tree.getVersion('models/user.ts'), 0)
})
})
Loading

0 comments on commit 117f9c1

Please sign in to comment.