Skip to content

Commit

Permalink
Issue/3 - Introduce cache worker middleware (#4) (#5)
Browse files Browse the repository at this point in the history
* Introduces validate cache worker middleware. #2

* Version bump. #2
  • Loading branch information
alexstandiford authored Sep 9, 2021
1 parent cf65881 commit ec710f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
33 changes: 33 additions & 0 deletions app/middlewares/setup/validateCacheWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { clearCache } from 'nicholas-router'
import fetch from 'nicholas-wp'

function check() {
new Promise( async ( res, rej ) => {
const status = await fetch( { path: '/nicholas/v1/cache-status' } )
const cacheStatus = JSON.parse( window.sessionStorage.getItem( 'cacheStatus' ) )
let cleared = false


if ( status.post_last_updated !== cacheStatus?.post_last_updated ) {
cleared = true
} else if ( status.theme_last_updated !== cacheStatus?.theme_last_updated ) {
cleared = true
}

if ( true === cleared ) {
console.debug( 'Cache was cleared' )
clearCache()
window.sessionStorage.setItem( 'cacheStatus', JSON.stringify( status ) )
} else {
console.debug( 'Cache was checked, but did not need cleared' )
}
} )
}

export default function ( args, next ) {
check()
// Check cache every 5 minutes
window.setInterval( check, 300000 )

next()
}
4 changes: 3 additions & 1 deletion middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import validateAdminPage from './app/middlewares/route/validateAdminPage'
import validateCompatibilityMode from './app/middlewares/route/validateCompatibilityMode'
import primeCache from './app/middlewares/route/primeCache'
import setPreloadWorker from './app/middlewares/setup/setPreloadWorker'
import validateCacheWorker from './app/middlewares/setup/validateCacheWorker'

// This should be compiled and exported as a global object.
export {
updateAdminBar,
validateAdminPage,
validateCompatibilityMode,
primeCache,
setPreloadWorker
setPreloadWorker,
validateCacheWorker
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nicholas-wp",
"version": "1.0.6",
"version": "1.0.7",
"description": "WordPress support for Nicholas nearly headless theme framework",
"main": "fetch.js",
"scripts": {
Expand Down

0 comments on commit ec710f4

Please sign in to comment.