Comments (9)
Yeah, for sure! It's up to you if you'd like to contribute. Ping me if you want to discuss anything. 😄
from next.js.
But why this test pass?
Next.js version for test environment : Next.js 14.2.0-canary.48
![스크린샷 2024-03-29 13 40 26](https://private-user-images.githubusercontent.com/33178048/317954908-8237d9f9-de89-4e12-a5f5-6fed29d6a884.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTcwOTE0OTEsIm5iZiI6MTcxNzA5MTE5MSwicGF0aCI6Ii8zMzE3ODA0OC8zMTc5NTQ5MDgtODIzN2Q5ZjktZGU4OS00ZTEyLWE1ZjUtNmZlZDI5ZDZhODg0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA1MzAlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNTMwVDE3NDYzMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM5NWE5NDAxYjQ1NDA2Yjc5MDEzOTFkMWVjMDY2NTZlMmEzZTRiNzBkOTIwNDdhMDJlZTQwNGNlNjBkZmQ5YzgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.llAX_rFcpZduLbectOCJUYTTWvbLq-1LEgw8_IJw-pw)
Repro
https://github.com/ojj1123/next-custom-loader-issue-repro/tree/with-next-canary
So I try to upgrade nextjs version to Next.js 14.2.0-canary.48
and then reproduce it, but the error occurs.
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103
Available memory (MB): 8192
Available CPU cores: 8
Binaries:
Node: 18.19.0
npm: 10.2.3
Yarn: 1.22.21
pnpm: 8.15.5
Relevant Packages:
next: 14.2.0-canary.48 // Latest available version is detected (14.2.0-canary.48).
eslint-config-next: 14.1.4
react: 18.2.0
react-dom: 18.2.0
typescript: 5.4.3
Next.js Config:
output: N/A
from next.js.
I found the reason.
Nextjs docs describes that the loader file must ONLY export a default function
so I did export as a default and then resolve this issue.
https://github.com/ojj1123/next-custom-loader-issue-repro/tree/export-default-loader
diff --git a/custom-loader.ts b/custom-loader.ts
index 461b1e3..d0af4f9 100644
--- a/custom-loader.ts
+++ b/custom-loader.ts
@@ -2,6 +2,10 @@
import { ImageLoaderProps } from 'next/image';
-export function customLoader({ src, width, quality }: ImageLoaderProps) {
+export default function customLoader({
+ src,
+ width,
+ quality,
+}: ImageLoaderProps) {
return `${src}?url=${encodeURIComponent(src)}&w=${width}&q=${quality || 75}`;
}
Suggestion
BTW, How about supporting a named export
?
some people(also me) use the eslint rule, import/no-default-export
. Currenly whenever using the custom loader, I have to turn off this rule manually. For flexibility, I suggest supporting a named export
for loader.
from next.js.
Hi, I'd say it is similar to how the next.config.js
is imported, where it also requires a default export.
Since Next.js doesn't support multiple loaders, it is hard to target which one to import with named exports.
Glad you found out the cause by your own!
from next.js.
Since Next.js doesn't support multiple loaders, it is hard to target which one to import with named exports.
Yeah, I agree with you.
Nextjs replaces defaultLoader
with custom loader (images.loaderFile
) through webpack config.resolve.alias
next.js/packages/next/src/build/create-compiler-aliases.ts
Lines 112 to 119 in b434ea4
next.js/packages/next/src/shared/lib/image-external.tsx
Lines 7 to 8 in 29d53c8
But I think it would be good to warn users not to use named exports. Is it possible?
from next.js.
Yeah, an accurate error message is always preferred. Mind if I open a PR? Feel free to open one if you'd like to!
from next.js.
Oh, so Could I try to PR? I want to investigate and resolve this issue!
from next.js.
UPDATE
It's easier to check it during runtime. Because if checking it during buildtime, i should parse the code of a loader file so it's a bit complex. So I've decided to deal with it runtime.
I try to resolve this issue but I want to discuss about this issue.
My tries were to detect during runtime and buildtime
1. During runtime
After build (dev
or build
script), Webpack replaces defaultLoader
with custom loader
module. And then If exporting custom loader as named, _imageLoader.default
(build result) is undefined
. So I try to write this condition statements.
import defaultLoader from '.....'
if(typeof defaultLoader === 'undefined') {
throw new Error('you should export the loader function as default');
}
It was working well. But I thought it's a bit hacky, because if doing that, I write that code wherever Next.js import defaultLoader
. (Of course, just two now)
So I try to check it and throw the error during build.
2. During buildtime
I try to make the custom webpack loader for allowing our to use a default function.
// /webpack/loader/next-image-loader-file-loader.ts
const nextImageLoaderFileLoader: webpack.LoaderDefinitionFunction = function (
this,
source
) {
const hasDefaultExport = /export default/.test(source)
if (!hasDefaultExport) {
throw new Error('you should export the loader function as default')
}
return source
}
// webpack.config.ts
...
module: {
rules: [
...(config.images.loaderFile
? [
{
test: config.images.loaderFile,
use: ['next-image-loader-file-loader'],
},
]
: []),
]
}
...
It was throwing the error well. But I thought it has some edge case:
function customLoader() {...}
export default customLoader; // correct
export customLoader; // throw error
export { customLoader as default } // throw error.. but this case is also default export so it should have not to throw the error!!
It's a bit difficult to check whether or not default function during either runtime or buildtime.
How do you think? Do you come up with another solution?
from next.js.
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.
from next.js.
Related Issues (20)
- Please stop auto closing bugs like #50388! Don't put all the burden on reporters HOT 1
- Fast Refresh Failing to Work on New Project HOT 3
- NextJS 14 HMR Tailwind CSS reloading not working HOT 4
- app router - dynamic twitter card image generator file does not return anything if returned div has style: justifyContent: "end". HOT 4
- Docs: Misleading and potentially vulnerable example code of getting client's IP address HOT 4
- Error name is always overridden with "Error" HOT 1
- Problem with postcss transformation when importing css with layer HOT 1
- ReactRemoveProperties dosen't work as it was
- Turbopack dev server and HMR consuming too much memory and freezing at code changes.
- TypeError: Failed to parse URL from /api/images HOT 7
- Vercel + pages router + basePath + middleware - Full roundtrip when client navigating to index page HOT 2
- Refractor languages missing when Turbopack is enabled
- "trailingSlash" causing CORS errors HOT 1
- Version 15 breaks select form element
- Docs: Versioned Documentation HOT 3
- Docs: inaccuracy in React Foundations (Chapter 4) HOT 3
- Sitemap is being generated incorrectly and an error in development also appears when I try to access /sitemap.xml HOT 13
- Cannot use Sharp in v15.0.0-canary.1 HOT 11
- NextJS 14 returns fetch failed with UND_ERR_CONNECT_TIMEOUT error on serverless function HOT 24
- enable swcMinify compression exception { toString, fromString } => {fromString} HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next.js.