Skip to content

Commit

Permalink
feat: cachetime command line option
Browse files Browse the repository at this point in the history
  • Loading branch information
BCsabaEngine committed Jan 6, 2025
1 parent 44639b9 commit 970a4dd
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change log

## 1.7

### 1.7.0

- Using the cachetime command line option, you can set whether the browser can cache pages. Together with the ETag switch, you can replace the default no-cache response with a max-age=xy response.

## 1.6

### 1.6.1
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ In order to be able to easily update OTA, it is important - from the users' poin

This npm package provides a solution for **inserting any JS client application into the ESP web server** (PsychicHttp and also ESPAsyncWebServer available, PsychicHttp is the default). For this, JS, html, css, font, assets, etc. files must be converted to binary byte array. Npm mode is easy to use and easy to **integrate into your CI/CD pipeline**.

> Starting with version v1.7.0, with the cachetime command line option, you can set whether the browser can cache pages
> Starting with version v1.6.0, mime npm package is used instead of mime-types (application/javascript -> text/javascript)
> Starting with version v1.5.0, PsychicHttp v2 is also supported.
Expand Down Expand Up @@ -176,6 +178,12 @@ The use of ETag is **not enabled by default**, this can be achieved with the `--

> This setting has three states: yes, no, and compiler mode is available. In compiler mode, you can disable/enable ETag by setting the `SVELTEESP32_ENABLE_ETAG` c++ compiler directive. For example, if using platformio, just type `-D SVELTEESP32_ENABLE_ETAG`.
### Cache-control

By default (when using the ETag), we send no-cache in the cache-control header of the HTTP response. Pages, subpages and other elements are downloaded every time. This is perfectly acceptable when serving small pages with ESP.

At the same time, it can be an advantage that the content is cached by the browser and not even the ETag check is performed. For this, you can specify how many seconds the max-age value sent instead of no-cache should be. In the case of `--cachetime=86400` (max-age=86400), the page (and other elements) will not be downloaded by the browser **for one day**.

### Main entry point - index.html

Typically, the entry point for web applications is the **index.htm or index.html** file. This does not need to be listed in the browser's address bar because web servers know that this file should be served by default. Svelteesp32 also does this: if there is an index.htm or index.html file, it sets it as the main file to be served. So using `http://esp_xxx.local` or just entering the `http://x.y.w.z/` IP address will serve this main file.
Expand Down Expand Up @@ -230,6 +238,7 @@ You can use the following c++ directives at the project level if you want to con
| `-e` | The engine for which the include file is created (psychic/psychic2/async) | psychic |
| `-o` | Generated output file with path | `svelteesp32.h` |
| `--etag` | Use ETag header for cache (true/false/compiler) | false |
| `--cachetime` | Override no-cache response with a max-age=<cachetime> response | 0 |
| `--gzip` | Compress content with gzip (true/false/compiler) | true |
| `--created` | Include creation time | false |
| `--version` | Include a version string, `--version=v$npm_package_version` | '' |
Expand Down
1 change: 1 addition & 0 deletions demo/esp32/platformio.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[env]
platform = espressif32
board = wemos_d1_mini32
#board = lolin_s3_mini
framework = arduino


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelteesp32",
"version": "1.6.1",
"version": "1.7.0",
"description": "Convert Svelte (or any frontend) JS application to serve it from ESP32 webserver (PsychicHttp)",
"author": "BCsabaEngine",
"license": "ISC",
Expand Down
54 changes: 27 additions & 27 deletions package.script
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/_/svelteesp32async.h --etag=false --gzip=false
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/g/svelteesp32async.h --etag=false --gzip=true
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/gc/svelteesp32async.h --etag=false --gzip=compiler
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/e/svelteesp32async.h --etag=true --gzip=false
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/eg/svelteesp32async.h --etag=true --gzip=true
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/egc/svelteesp32async.h --etag=true --gzip=compiler
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/ec/svelteesp32async.h --etag=compiler --gzip=false
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/ecg/svelteesp32async.h --etag=compiler --gzip=true
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/ecgc/svelteesp32async.h --etag=compiler --gzip=compiler
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/_/svelteesp32async.h --etag=false --gzip=false --cachetime=86400
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/g/svelteesp32async.h --etag=false --gzip=true --cachetime=86400
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/gc/svelteesp32async.h --etag=false --gzip=compiler --cachetime=86400
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/e/svelteesp32async.h --etag=true --gzip=false --cachetime=86400
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/eg/svelteesp32async.h --etag=true --gzip=true --cachetime=86400
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/egc/svelteesp32async.h --etag=true --gzip=compiler --cachetime=86400
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/ec/svelteesp32async.h --etag=compiler --gzip=false --cachetime=86400
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/ecg/svelteesp32async.h --etag=compiler --gzip=true --cachetime=86400
npx tsx src/index.ts -e async -s ./demo/svelte/dist -o ./demo/esp32/include/ecgc/svelteesp32async.h --etag=compiler --gzip=compiler --cachetime=86400

npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/_/svelteesp32psychic.h --etag=false --gzip=false
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/g/svelteesp32psychic.h --etag=false --gzip=true
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/gc/svelteesp32psychic.h --etag=false --gzip=compiler
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/e/svelteesp32psychic.h --etag=true --gzip=false
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/eg/svelteesp32psychic.h --etag=true --gzip=true
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/egc/svelteesp32psychic.h --etag=true --gzip=compiler
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/ec/svelteesp32psychic.h --etag=compiler --gzip=false
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/ecg/svelteesp32psychic.h --etag=compiler --gzip=true
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/ecgc/svelteesp32psychic.h --etag=compiler --gzip=compiler
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/_/svelteesp32psychic.h --etag=false --gzip=false --cachetime=86400
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/g/svelteesp32psychic.h --etag=false --gzip=true --cachetime=86400
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/gc/svelteesp32psychic.h --etag=false --gzip=compiler --cachetime=86400
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/e/svelteesp32psychic.h --etag=true --gzip=false --cachetime=86400
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/eg/svelteesp32psychic.h --etag=true --gzip=true --cachetime=86400
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/egc/svelteesp32psychic.h --etag=true --gzip=compiler --cachetime=86400
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/ec/svelteesp32psychic.h --etag=compiler --gzip=false --cachetime=86400
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/ecg/svelteesp32psychic.h --etag=compiler --gzip=true --cachetime=86400
npx tsx src/index.ts -e psychic -s ./demo/svelte/dist -o ./demo/esp32/include/ecgc/svelteesp32psychic.h --etag=compiler --gzip=compiler --cachetime=86400

npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/_/svelteesp32psychic2.h --etag=false --gzip=false
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/g/svelteesp32psychic2.h --etag=false --gzip=true
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/gc/svelteesp32psychic2.h --etag=false --gzip=compiler
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/e/svelteesp32psychic2.h --etag=true --gzip=false
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/eg/svelteesp32psychic2.h --etag=true --gzip=true
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/egc/svelteesp32psychic2.h --etag=true --gzip=compiler
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/ec/svelteesp32psychic2.h --etag=compiler --gzip=false
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/ecg/svelteesp32psychic2.h --etag=compiler --gzip=true
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/ecgc/svelteesp32psychic2.h --etag=compiler --gzip=compiler
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/_/svelteesp32psychic2.h --etag=false --gzip=false --cachetime=86400
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/g/svelteesp32psychic2.h --etag=false --gzip=true --cachetime=86400
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/gc/svelteesp32psychic2.h --etag=false --gzip=compiler --cachetime=86400
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/e/svelteesp32psychic2.h --etag=true --gzip=false --cachetime=86400
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/eg/svelteesp32psychic2.h --etag=true --gzip=true --cachetime=86400
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/egc/svelteesp32psychic2.h --etag=true --gzip=compiler --cachetime=86400
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/ec/svelteesp32psychic2.h --etag=compiler --gzip=false --cachetime=86400
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/ecg/svelteesp32psychic2.h --etag=compiler --gzip=true --cachetime=86400
npx tsx src/index.ts -e psychic2 -s ./demo/svelte/dist -o ./demo/esp32/include/ecgc/svelteesp32psychic2.h --etag=compiler --gzip=compiler --cachetime=86400
6 changes: 6 additions & 0 deletions src/commandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ICopyFilesArguments {
define: string;
gzip: 'true' | 'false' | 'compiler';
etag: 'true' | 'false' | 'compiler';
cachetime: number;
created: boolean;
version: string;
help?: boolean;
Expand Down Expand Up @@ -79,6 +80,11 @@ export const cmdLine = parse<ICopyFilesArguments>(
description: 'Prefix of c++ defines',
defaultValue: 'SVELTEESP32'
},
cachetime: {
type: Number,
description: 'max-age cache time in seconds',
defaultValue: 0
},
help: { type: Boolean, optional: true, alias: 'h', description: 'Shows this help' }
},
{
Expand Down
31 changes: 31 additions & 0 deletions src/cppCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,22 @@ void {{methodName}}(PsychicHttpServer * server) {
{{#switch ../etag}}
{{#case "true"}}
{{#../cacheTime}}
response.addHeader("cache-control", "max-age={{value}}");
{{/../cacheTime}}
{{^../cacheTime}}
response.addHeader("cache-control", "no-cache");
{{/../cacheTime}}
response.addHeader("ETag", etag_{{this.dataname}});
{{/case}}
{{#case "compiler"}}
#ifdef {{../definePrefix}}_ENABLE_ETAG
{{#../cacheTime}}
response.addHeader("cache-control", "max-age={{value}}");
{{/../cacheTime}}
{{^../cacheTime}}
response.addHeader("cache-control", "no-cache");
{{/../cacheTime}}
response.addHeader("ETag", etag_{{this.dataname}});
#endif
{{/case}}
Expand Down Expand Up @@ -345,12 +355,22 @@ void {{methodName}}(PsychicHttpServer * server) {
{{#switch ../etag}}
{{#case "true"}}
{{#../cacheTime}}
response->addHeader("cache-control", "max-age={{value}}");
{{/../cacheTime}}
{{^../cacheTime}}
response->addHeader("cache-control", "no-cache");
{{/../cacheTime}}
response->addHeader("ETag", etag_{{this.dataname}});
{{/case}}
{{#case "compiler"}}
#ifdef {{../definePrefix}}_ENABLE_ETAG
{{#../cacheTime}}
response->addHeader("cache-control", "max-age={{value}}");
{{/../cacheTime}}
{{^../cacheTime}}
response->addHeader("cache-control", "no-cache");
{{/../cacheTime}}
response->addHeader("ETag", etag_{{this.dataname}});
#endif
{{/case}}
Expand Down Expand Up @@ -526,12 +546,22 @@ void {{methodName}}(AsyncWebServer * server) {
{{#switch ../etag}}
{{#case "true"}}
{{#../cacheTime}}
response->addHeader("cache-control", "max-age={{value}}");
{{/../cacheTime}}
{{^../cacheTime}}
response->addHeader("cache-control", "no-cache");
{{/../cacheTime}}
response->addHeader("ETag", etag_{{this.dataname}});
{{/case}}
{{#case "compiler"}}
#ifdef {{../definePrefix}}_ENABLE_ETAG
{{#../cacheTime}}
response->addHeader("cache-control", "max-age={{value}}");
{{/../cacheTime}}
{{^../cacheTime}}
response->addHeader("cache-control", "no-cache");
{{/../cacheTime}}
response->addHeader("ETag", etag_{{this.dataname}});
#endif
{{/case}}
Expand Down Expand Up @@ -572,6 +602,7 @@ export const getCppCode = (sources: CppCodeSources, filesByExtension: ExtensionG
created: cmdLine.created,
version: cmdLine.version,
methodName: cmdLine.espmethod,
cacheTime: cmdLine.cachetime ? { value: cmdLine.cachetime } : undefined,
definePrefix: cmdLine.define
},
{
Expand Down

0 comments on commit 970a4dd

Please sign in to comment.