Skip to content

Commit

Permalink
add PWA proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszduda23 committed Jul 13, 2024
1 parent 4bf74e3 commit 0adb88e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
3 changes: 2 additions & 1 deletion packages/v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"vite": "^2.3.6",
"vite-plugin-html": "^2.1.1",
"vite-plugin-package-version": "^1.0.2",
"vite-plugin-singlefile": "^0.5.1"
"vite-plugin-singlefile": "^0.5.1",
"vite-plugin-pwa": "^0.12.8"
}
}
15 changes: 14 additions & 1 deletion packages/v2/src/esp-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface Config {
export default class EspApp extends LitElement {
@state() scheme: string = "";
@state() ping: string = "";
@state() redirectUrl: string = "";
@query("#beat")
beat!: HTMLSpanElement;

Expand Down Expand Up @@ -119,9 +120,21 @@ export default class EspApp extends LitElement {
: nothing;
}

handleRedirect() {
window.location.href = "?target=" + this.redirectUrl;
}

render() {
return html`
<h1>
<div class="redirect-container">
<input
type="text"
placeholder="esphome address"
@input="${(e: Event) => (this.redirectUrl = (e.target as HTMLInputElement).value)}"
/>
<button class="btn" @click="${this.handleRedirect}">Go</button>
</div>
<h1>
<a href="https://esphome.io/web-api" class="logo">
<esp-logo style="width: 52px; height: 40px; display: block;"></esp-logo>
</a>
Expand Down
10 changes: 8 additions & 2 deletions packages/v2/src/esp-entity-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ interface entityConfig {
}

export function getBasePath() {
let str = window.location.pathname;
return str.endsWith("/") ? str.slice(0, -1) : str;
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const target = urlParams.get('target')
if (target !== null) {
return "http://" + target
}
const pathname = window.location.pathname;
return pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;;
}

let basePath = getBasePath();
Expand Down
30 changes: 11 additions & 19 deletions packages/v2/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { viteSingleFile } from "vite-plugin-singlefile";
import { minifyHtml as ViteMinifyHtml } from "vite-plugin-html";
import stripBanner from "rollup-plugin-strip-banner";
import replace from "@rollup/plugin-replace";

const proxy_target = process.env.PROXY_TARGET || "http://nodemcu.local";
import { VitePWA } from 'vite-plugin-pwa'

export default defineConfig({
clearScreen: false,
Expand Down Expand Up @@ -53,6 +52,15 @@ export default defineConfig({
enforce: "post",
apply: "build",
},
VitePWA({
registerType: 'autoUpdate',
manifest: {
icons: [{
"src": "logo.svg",
"sizes": "any"
}],
}
}),
],
build: {
brotliSize: false,
Expand All @@ -71,24 +79,8 @@ export default defineConfig({
},
},
server: {
open: "/", // auto open browser in dev mode
host: true, // dev on local and network
host: true,
port: 5001,
strictPort: true,
proxy: {
"/light": proxy_target,
"/select": proxy_target,
"/cover": proxy_target,
"/switch": proxy_target,
"/button": proxy_target,
"/fan": proxy_target,
"/lock": proxy_target,
"/number": proxy_target,
"/climate": proxy_target,
"/events": proxy_target,
"/text": proxy_target,
"/date": proxy_target,
"/time": proxy_target,
},
},
});

0 comments on commit 0adb88e

Please sign in to comment.