自定义 Next.js 服务器的 MIME 类型错误
Wrong MIME type with Custom Next.js Server
我正在编写一个带有自定义 server.js 文件的 Next.js 应用程序,但我无法加载我的 css - 我在浏览器控制台中不断收到以下信息:
The resource from “http://localhost:3000/_next/
static/development/pages/index.js?ts=1552499710032”
was blocked due to MIME type (“text/html”)
mismatch (X-Content-Type-Options: nosniff)
我也不知道为什么。据我所知,我已经像为以前的工作版本所做的那样配置了我的应用程序。这是我的 next.config.js 文件:
const withCSS = require('@zeit/next-css');
const withImages = require('next-images');
const path = require('path')
const Dotenv = require('dotenv-webpack')
require('dotenv').config()
module.exports = withImages(withCSS({
webpack: config => {
// Fixes npm packages that depend on `fs` module
config.node = {
fs: 'empty'
}
config.plugins = config.plugins || []
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true
})
]
return config
}
}))
这是我的 server.js 文件:
const express = require('express')
const next = require('next')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const path = require('path');
const options = {
root: path.join(__dirname, '/static'),
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
}
};
app.prepare().then(() => {
const server = express()
server.get('/robots.txt', (req, res) => {
return res.status(200).sendFile('robots.txt', options)
});
server.get('/sitemap.xml', (req,res) => {
return res.status(200).sendFile('sitemap.xml', options)
});
server.get('/', (req, res) => {
return app.render(req, res, '/', req.query)
})
server.listen(port, err => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})
我使用 node server.js
运行 我的应用程序,我使用 import "./styles/root.css"
导入 css (我唯一的 css 文件)所以没有惊喜那里。出了什么问题?
编辑:这是微不足道的,它可能是一个错误。我在这里打开了一份报告:https://github.com/zeit/next.js/issues/6647.
你不见了
server.get('*', (req, res) => {
return handle(req, res)
})
在你的 server.js 中。如果您取消注释上述代码并重新启动服务器,您的 github 存储库将正常工作
我正在编写一个带有自定义 server.js 文件的 Next.js 应用程序,但我无法加载我的 css - 我在浏览器控制台中不断收到以下信息:
The resource from “http://localhost:3000/_next/
static/development/pages/index.js?ts=1552499710032”
was blocked due to MIME type (“text/html”)
mismatch (X-Content-Type-Options: nosniff)
我也不知道为什么。据我所知,我已经像为以前的工作版本所做的那样配置了我的应用程序。这是我的 next.config.js 文件:
const withCSS = require('@zeit/next-css');
const withImages = require('next-images');
const path = require('path')
const Dotenv = require('dotenv-webpack')
require('dotenv').config()
module.exports = withImages(withCSS({
webpack: config => {
// Fixes npm packages that depend on `fs` module
config.node = {
fs: 'empty'
}
config.plugins = config.plugins || []
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true
})
]
return config
}
}))
这是我的 server.js 文件:
const express = require('express')
const next = require('next')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const path = require('path');
const options = {
root: path.join(__dirname, '/static'),
headers: {
'Content-Type': 'text/plain;charset=UTF-8',
}
};
app.prepare().then(() => {
const server = express()
server.get('/robots.txt', (req, res) => {
return res.status(200).sendFile('robots.txt', options)
});
server.get('/sitemap.xml', (req,res) => {
return res.status(200).sendFile('sitemap.xml', options)
});
server.get('/', (req, res) => {
return app.render(req, res, '/', req.query)
})
server.listen(port, err => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})
我使用 node server.js
运行 我的应用程序,我使用 import "./styles/root.css"
导入 css (我唯一的 css 文件)所以没有惊喜那里。出了什么问题?
编辑:这是微不足道的,它可能是一个错误。我在这里打开了一份报告:https://github.com/zeit/next.js/issues/6647.
你不见了
server.get('*', (req, res) => {
return handle(req, res)
})
在你的 server.js 中。如果您取消注释上述代码并重新启动服务器,您的 github 存储库将正常工作