簡體   English   中英

動態導入不適用於組合的CRA和SSR

[英]Dynamic import doesn't work with combined CRA and SSR

我已經使用create-react-app創建了一個網站,由於seo,現在我想使用服務器端渲染。

經過長期的研究,我在這里找到了一個簡單的工具。

它工作正常,但是在我的代碼中,我有一個動態導入await import("localization/" + newLanguage + "/" + newLanguage + ".json"); 因此,我得到了SyntaxError: Unexpected token import錯誤。

我也嘗試使用System.import 這對SSR 'System.import' is restricted from being used. Please use import() instead.但是CRA說'System.import' is restricted from being used. Please use import() instead. 'System.import' is restricted from being used. Please use import() instead.

我怎么解決這個問題? 我不想彈出或類似並保持它的簡單。

我的index.js:

 require('ignore-styles'); require('babel-register')({ ignore: /\\/(build|node_modules)\\//, presets: ['env','react-app'], plugins: ["syntax-dynamic-import"] }); require('./server'); 

這是我的服務器文件:

 import express from 'express'; import http from 'http'; import React from 'react'; import ReactDOMServer from 'react-dom/server'; import { StaticRouter } from "react-router"; import App from "./src/App"; const port = 3000; const app = express(); const server = http.createServer(app); app.use((req, res) => { const context = {} const html = ReactDOMServer.renderToString( <StaticRouter location={req.url} context={context}> <App /> </StaticRouter> ); if(context.url){ res.status(302).redirect(context.url); return; } res.status(200).send(html); }); server.listen(port, function(){ console.log('Node server running on port ' + port + '.'); console.log("Time: " + new Date(Date.now())); }); 

好。 我懂了。

我使用了來自babel的語法動態導入插件。 使用airbnb的dynamic-import-node可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM