Skip to content

Commit

Permalink
Add command line option to disable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
GRA0007 committed Jul 2, 2022
1 parent 60bbcb8 commit f28be82
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ target Path to static directory
routeFile Path to cjs router script (can use ES6 with --build option)
Options:
--help Show help [boolean]
--version Show version number [boolean]
-p, --port port to use for http server [string] [default: 8080]
-h, --host host to use for http server [string] [default: "0.0.0.0"]
-i, --index path to index html inside of target [string] [default: "index.html"]
-b, --build build routes file in memory [boolean] [default: false]
--help Show help [boolean]
--version Show version number [boolean]
-p, --port port to use for http server [string] [default: 8080]
-h, --host host to use for http server [string] [default: "0.0.0.0"]
-i, --index path to index html inside of target [string] [default: "index.html"]
-b, --build build routes file in memory [boolean] [default: false]
--cache use --no-cache to disable all route caching [boolean] [default: true]
```

#### `epoxy build`
Expand Down Expand Up @@ -148,7 +149,7 @@ export default {
}
```

If you include a `key` that is not undefined, then the result of the handler function will be cached based on that key. You can also include a time to live `ttl` parameter which will expire the cache after a certain amount of milliseconds.
If you include a `key` that is not undefined, then the result of the handler function will be cached based on that key. You can also include a time to live `ttl` parameter which will expire the cache after a certain amount of milliseconds. There is also a command line argument `--no-cache` that will disable all caching irrespective of any keys provided.

Each route must have a function to handle it, which will receive a `request` object from ExpressJS, from which you can learn about the request. See the express docs for the [request object](https://expressjs.com/en/api.html#req) for more information.

Expand Down
8 changes: 7 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Parcel } from '@parcel/core'
import loadRoutes from './src/loadRoutes'
import startServer, { Routes } from './src/server'

const serve = async ({ target, routeFile, host, port, index, build }) => {
const serve = async ({ target, routeFile, host, port, index, build, cache }) => {
// Load routes
let routes: Routes = {}
if (routeFile) {
Expand All @@ -30,6 +30,7 @@ const serve = async ({ target, routeFile, host, port, index, build }) => {
target,
routes,
index: path.join(target, index),
cache,
})
}

Expand Down Expand Up @@ -91,6 +92,11 @@ yargs
type: 'boolean',
description: 'build routes file in memory',
default: false,
})
.option('cache', {
type: 'boolean',
description: 'use --no-cache to disable all route handler result caching',
default: true,
}),
serve
)
Expand Down
8 changes: 5 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ const applyHandler = async (
req: Request,
value: RouteHandler,
indexText: string,
cacheEnabled: boolean,
) => {
const { handler, key, ttl } = await routeHandlerAsObject(value)

const resolvedKey = await resolveKey(key, req)
const keyValue = resolvedKey && `${route}-${JSON.stringify(resolvedKey)}`

const cachedResult = keyValue && await cache.get(keyValue)
const cachedResult = cacheEnabled && keyValue && await cache.get(keyValue)
if (cachedResult) {
return injectHTML(indexText, cachedResult)
} else {
const handlerResult = await handler(req)
.catch(e => console.warn(`Handler for route ${route} threw an error`, e))

// Save result in cache if key set
if (keyValue && handlerResult) {
if (cacheEnabled && keyValue && handlerResult) {
await cache.set(keyValue, handlerResult, ttl)
}

Expand All @@ -58,6 +59,7 @@ const startServer = async ({
index = './dist/index.html',
port,
routes,
cache = true,
}) => {
// Resolve paths
const resolvedTarget = path.resolve(target)
Expand All @@ -75,7 +77,7 @@ const startServer = async ({
// Register dynamic routes
Object.entries(routes as Routes).forEach(([route, handler]) => {
app.get(route, async (req, res) => {
const injectedIndex = await applyHandler(route, req, handler, indexText)
const injectedIndex = await applyHandler(route, req, handler, indexText, cache)
return res.header('Content-Type', 'text/html').send(injectedIndex)
})
})
Expand Down

0 comments on commit f28be82

Please sign in to comment.