-
Notifications
You must be signed in to change notification settings - Fork 318
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(nextjs): [improved] Hint correct middleware location when missing clerkMiddleware #5028
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
19d7256
Revert "Revert "feat(nextjs): Hint correct middleware location when m…
panteliselef 648f2e8
feat(nextjs): [improved] Hint correct middleware location when missin…
panteliselef 1eef575
add changeset
panteliselef c5dd682
extract to other files
panteliselef 3f9c985
handle the .js extension
panteliselef 1770c22
improve readability of suggestMiddlewareLocation
panteliselef 344d0bc
Merge branch 'main' into elef/fix-hint-middleware
panteliselef a7a129d
add comments
panteliselef a244047
Merge branch 'main' into elef/fix-hint-middleware
panteliselef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clerk/nextjs': patch | ||
--- | ||
|
||
Bug fix: Remove warning for accessing Node APIs when running `next build` with `clerkMiddleware` imported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { nodeCwdOrThrow, nodeFsOrThrow, nodePathOrThrow } from './utils'; | ||
|
||
function hasSrcAppDir() { | ||
const { existsSync } = nodeFsOrThrow(); | ||
const path = nodePathOrThrow(); | ||
const cwd = nodeCwdOrThrow(); | ||
|
||
const projectWithAppSrc = path.join(cwd(), 'src', 'app'); | ||
|
||
return !!existsSync(projectWithAppSrc); | ||
} | ||
|
||
function suggestMiddlewareLocation() { | ||
const fileExtensions = ['ts', 'js'] as const; | ||
const suggestionMessage = ( | ||
extension: (typeof fileExtensions)[number], | ||
to: 'src/' | '', | ||
from: 'src/app/' | 'app/' | '', | ||
) => | ||
`Clerk: clerkMiddleware() was not run, your middleware file might be misplaced. Move your middleware file to ./${to}middleware.${extension}. Currently located at ./${from}middleware.${extension}`; | ||
|
||
const { existsSync } = nodeFsOrThrow(); | ||
const path = nodePathOrThrow(); | ||
const cwd = nodeCwdOrThrow(); | ||
|
||
const projectWithAppSrcPath = path.join(cwd(), 'src', 'app'); | ||
const projectWithAppPath = path.join(cwd(), 'app'); | ||
|
||
const checkMiddlewareLocation = ( | ||
basePath: string, | ||
to: 'src/' | '', | ||
from: 'src/app/' | 'app/' | '', | ||
): string | undefined => { | ||
for (const fileExtension of fileExtensions) { | ||
if (existsSync(path.join(basePath, `middleware.${fileExtension}`))) { | ||
return suggestionMessage(fileExtension, to, from); | ||
} | ||
} | ||
return undefined; | ||
}; | ||
|
||
if (existsSync(projectWithAppSrcPath)) { | ||
return ( | ||
checkMiddlewareLocation(projectWithAppSrcPath, 'src/', 'src/app/') || checkMiddlewareLocation(cwd(), 'src/', '') | ||
); | ||
} | ||
|
||
if (existsSync(projectWithAppPath)) { | ||
return checkMiddlewareLocation(projectWithAppPath, '', 'app/'); | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
export { suggestMiddlewareLocation, hasSrcAppDir }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Attention: Only import this module when the node runtime is used. | ||
* We are using conditional imports to mitigate bundling issues with Next.js server actions on version prior to 14.1.0. | ||
*/ | ||
// @ts-ignore | ||
import nodeRuntime from '#safe-node-apis'; | ||
|
||
const throwMissingFsModule = (module: string) => { | ||
throw new Error(`Clerk: ${module} is missing. This is an internal error. Please contact Clerk's support.`); | ||
}; | ||
|
||
const nodeFsOrThrow = () => { | ||
if (!nodeRuntime.fs) { | ||
throwMissingFsModule('fs'); | ||
} | ||
return nodeRuntime.fs; | ||
}; | ||
|
||
const nodePathOrThrow = () => { | ||
if (!nodeRuntime.path) { | ||
throwMissingFsModule('path'); | ||
} | ||
return nodeRuntime.path; | ||
}; | ||
|
||
const nodeCwdOrThrow = () => { | ||
if (!nodeRuntime.cwd) { | ||
throwMissingFsModule('cwd'); | ||
} | ||
return nodeRuntime.cwd; | ||
}; | ||
|
||
export { nodeCwdOrThrow, nodeFsOrThrow, nodePathOrThrow }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add comment here to clarify why we have the split
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done