Preact:有时在重新加载时出现空白页
Preact: sometimes blank page upon reload
我对 Preact 和 TypeScript 很陌生,使用 parcel-preact-typescript-boilerplate
.
引导了一个应用程序
到目前为止一切正常,但我意识到有时在重新加载时我会得到一个空白页面。它也会在打开页面时不时发生。空白页是指我的 Hello
组件未在 document.body
中呈现。但是,当我在 Google Chrome 中禁用缓存时不会发生这种情况,当我在 Firefox 中禁用缓存时仍然会出现这种情况。
我认为这可能是我的代码更改和引入的原因,但是在克隆 parcel-preact-typescript-boilerplate
并以 npm run start
启动它时,我最终遇到了同样奇怪的行为。
这可能是什么原因?
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>PARCEL-PREACT-TYPESCRIPT-BOILERPLATE</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link async rel="stylesheet" href="./src/style.css" />
<script async src="./src/index.tsx"></script>
</head>
<body>
</body>
</html>
index.tsx:
import { h, render } from 'preact';
const Hello = () => (<div><h1 className="hello" >Hello World</h1></div>);
render(<Hello/>, document.body);
package.json:
{
"name": "parcel-preact-typescript-boilerplate",
"version": "1.0.6",
"description": "simple parcel preact typescript project without preact-compat",
"main": "index.html",
"scripts": {
"start": "parcel index.html",
"watch": "parcel watch index.html",
"build": "parcel build index.html",
"rebuild": "npm run clean && npm run build",
"clean": "rimraf dist/ && rimraf .cache"
},
"repository": {
"type": "git",
"url": "git+https://github.com/benevolarX/parcel-preact-typescript-boilerplate.git"
},
"keywords": [
"parcel",
"preact",
"typescript",
"boilerplate",
"project"
],
"author": "benevolar",
"license": "MIT",
"bugs": {
"url": "https://github.com/benevolarX/parcel-preact-typescript-boilerplate/issues"
},
"homepage": "https://github.com/benevolarX/parcel-preact-typescript-boilerplate#readme",
"devDependencies": {
"@babel/core": "^7.2.0",
"@types/node": "^10.12.12",
"babel-preset-preact": "^1.1.0",
"parcel-bundler": "^1.10.3",
"rimraf": "^2.6.2",
"typescript": "^3.2.2"
},
"dependencies": {
"preact": "^8.4.2",
"preact-compat": "^3.18.4"
}
}
tsconfig.json:
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"checkJs": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"jsx": "react",
"jsxFactory": "h",
"module": "commonjs",
"outDir": "dist",
"paths": {
"~*": ["./src/*"]
},
"pretty": true,
"removeComments": true,
"strict": true,
"target": "esnext",
},
"include": [
"src/**/*"
],
"parcelTsPluginOptions": {
"transpileOnly": false
}
}
编辑:
玩了一会儿后,我意识到在用 parcel 构建所有内容并通过 preact serve dist
/preact watch dist/index.html
而不是 parcel index.html
启动应用程序后,问题不会发生
您(或包裹)正在尝试加载浏览器不喜欢的 Typescript (TSX) 文件。变化
<script async src="./src/index.tsx"></script>
到
<script async src="./[build folder]/index.js"></script>
它应该可以工作。我相信 parcel 将构建的脚本放在一个名为 build
的文件夹中,因此将其放在上面代码段中 [build folder]
的位置。
我对 Preact 和 TypeScript 很陌生,使用 parcel-preact-typescript-boilerplate
.
到目前为止一切正常,但我意识到有时在重新加载时我会得到一个空白页面。它也会在打开页面时不时发生。空白页是指我的 Hello
组件未在 document.body
中呈现。但是,当我在 Google Chrome 中禁用缓存时不会发生这种情况,当我在 Firefox 中禁用缓存时仍然会出现这种情况。
我认为这可能是我的代码更改和引入的原因,但是在克隆 parcel-preact-typescript-boilerplate
并以 npm run start
启动它时,我最终遇到了同样奇怪的行为。
这可能是什么原因?
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>PARCEL-PREACT-TYPESCRIPT-BOILERPLATE</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link async rel="stylesheet" href="./src/style.css" />
<script async src="./src/index.tsx"></script>
</head>
<body>
</body>
</html>
index.tsx:
import { h, render } from 'preact';
const Hello = () => (<div><h1 className="hello" >Hello World</h1></div>);
render(<Hello/>, document.body);
package.json:
{
"name": "parcel-preact-typescript-boilerplate",
"version": "1.0.6",
"description": "simple parcel preact typescript project without preact-compat",
"main": "index.html",
"scripts": {
"start": "parcel index.html",
"watch": "parcel watch index.html",
"build": "parcel build index.html",
"rebuild": "npm run clean && npm run build",
"clean": "rimraf dist/ && rimraf .cache"
},
"repository": {
"type": "git",
"url": "git+https://github.com/benevolarX/parcel-preact-typescript-boilerplate.git"
},
"keywords": [
"parcel",
"preact",
"typescript",
"boilerplate",
"project"
],
"author": "benevolar",
"license": "MIT",
"bugs": {
"url": "https://github.com/benevolarX/parcel-preact-typescript-boilerplate/issues"
},
"homepage": "https://github.com/benevolarX/parcel-preact-typescript-boilerplate#readme",
"devDependencies": {
"@babel/core": "^7.2.0",
"@types/node": "^10.12.12",
"babel-preset-preact": "^1.1.0",
"parcel-bundler": "^1.10.3",
"rimraf": "^2.6.2",
"typescript": "^3.2.2"
},
"dependencies": {
"preact": "^8.4.2",
"preact-compat": "^3.18.4"
}
}
tsconfig.json:
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"checkJs": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"jsx": "react",
"jsxFactory": "h",
"module": "commonjs",
"outDir": "dist",
"paths": {
"~*": ["./src/*"]
},
"pretty": true,
"removeComments": true,
"strict": true,
"target": "esnext",
},
"include": [
"src/**/*"
],
"parcelTsPluginOptions": {
"transpileOnly": false
}
}
编辑:
玩了一会儿后,我意识到在用 parcel 构建所有内容并通过 preact serve dist
/preact watch dist/index.html
而不是 parcel index.html
您(或包裹)正在尝试加载浏览器不喜欢的 Typescript (TSX) 文件。变化
<script async src="./src/index.tsx"></script>
到
<script async src="./[build folder]/index.js"></script>
它应该可以工作。我相信 parcel 将构建的脚本放在一个名为 build
的文件夹中,因此将其放在上面代码段中 [build folder]
的位置。