I am quite new to Preact & TypeScript and bootstrapped an application using parcel-preact-typescript-boilerplate
.
Everything works fine so far, but I realized that from time to time upon a reload I end up with a blank page. It also happens from time to time upon opening the page. By blank page I mean that my Hello
component does not render in the document.body
. However, this does not occur when I disable caching in Google Chrome, it still occurs when I disable caching in Firefox.
I thought the reason for this might be my code changes and introductions, but upon cloning parcel-preact-typescript-boilerplate
and just starting it with npm run start
I end up with the same weird behaviour.
What could be the reason for this?
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
}
}
edit:
After playing around a little bit I realized that the problem does not occur after building everything with parcel and starting the application by preact serve dist
/preact watch dist/index.html
instead of parcel index.html
You're (or parcel) is trying to load a Typescript (TSX) file, which the browser won't like. Change
<script async src="./src/index.tsx"></script>
to
<script async src="./[build folder]/index.js"></script>
And it should work. I believe parcel puts built scripts in a folder called build
so put that in the place of [build folder]
in the above snippet.