如何为外部URL正确设置下一个图像加载器URL [英] How to set up Next image loader url correctly for external url
本文介绍了如何为外部URL正确设置下一个图像加载器URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图在Firebase上托管我的第一个应用程序,但我在next.config文件上遇到了一些图像加载程序的问题。 初始配置如下
module.exports = {
images: {
domains: ['assets.coingecko.com'],
loader: 'imgix',
path: 'https://assets.coingecko.com/coins/images/',
},
当我检查部署页面上的URL时,链接指向
https://assets.coingecko.com/https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579=&auto=format&fit=max&w=64
但正确的url是
https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579=&auto=format&fit=max&w=64.
我已尝试将我的配置文件修改为以下
module.exports = {
images: {
domains: ['assets.coingecko.com'],
loader: 'imgix',
path: '/',
},
或
module.exports = {
images: {
domains: ['assets.coingecko.com'],
loader: 'imgix',
path: '',
},
但是,当我这样做并再次部署页面时,我收到错误500白色页面。
有没有人能帮帮新手?我不知道该怎么处理它。
完整的next.config.js代码段
const webpack = require('webpack');
const path = require('path');
//const withPlugins = require('next-compose-plugins');
//const optimizedImages = require('next-optimized-images');
//const withImages = require('next-images')
module.exports = {
images: {
domains: ['assets.coingecko.com'],
// loader: 'imgix',
// path: '',
},
reactStrictMode: true,
entry: './src/index.js',
sassOptions: {
includePaths: [path.join(__dirname, 'styles')],
},
module: {
rules: [
//...
{
test: /.(png|jp(e*)g|svg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'images/[hash]-[name].[ext]',
},
},
],
},
],
},
//...
}
server.js
const { https } = require('firebase-functions');
const { default: next } = require('next');
const isDev = process.env.NODE_ENV !== 'production';
const server = next({
dev: isDev,
//location of .next generated after running -> yarn build
conf: { distDir: '.next' },
//images :{ domain :['assets.coingecko.com'],}
});
const nextjsHandle = server.getRequestHandler();
exports.nextServer = https.onRequest((req, res) => {
return server.prepare()
.then(() => {
return nextjsHandle(req, res)
});
});
推荐答案
好吧,我设法绕过了@rawwater@seanw。在我的例子中,基本上需要做的是从下一个配置页面中删除加载器和路径,如下所示:
const webpack = require('webpack');
const path = require('path');
//const withPlugins = require('next-compose-plugins');
//const optimizedImages = require('next-optimized-images');
const withImages = require('next-images')
module.exports = withImages( {
images: {
domains: ['assets.coingecko.com', 'mywebsite.whatever.com'],
// loader: 'imgix',
// path: 'https://assets.coingecko.com/',
},
reactStrictMode: true,
entry: './src/index.js',
sassOptions: {
includePaths: [path.join(__dirname, 'styles')],
},
module: {
rules: [
//...
{
test: /.(png|jp(e*)g|svg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'images/[hash]-[name].[ext]',
},
},
],
},
],
},
//...
}
)
在server.js上取消注释图片,如下所示
const { https } = require('firebase-functions');
const { default: next } = require('next');
const isDev = process.env.NODE_ENV !== 'production';
const server = next({
dev: isDev,
//location of .next generated after running -> yarn build
conf: { distDir: '.next' },
//images :{ domain :['assets.coingecko.com'],}
});
const nextjsHandle = server.getRequestHandler();
exports.nextServer = https.onRequest((req, res) => {
return server.prepare()
.then(() => {
return nextjsHandle(req, res)
});
});
然后将自定义加载器添加到我的图像中,如下所示,该组件将呈现它们
const myLoader = ({ src, width, quality }) => {
return `${src}?w=${width}&q=${quality || 75}`
}
<Image
loader={myLoader}
className="mr-3"
src={image}
alt="crypto"
height={30}
width={30}
/>
和它:)。
感谢大家的帮助。我希望这将是未来的人。:)
这篇关于如何为外部URL正确设置下一个图像加载器URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文