Skip to content

Instantly share code, notes, and snippets.

@eneajaho
Last active November 8, 2022 16:17
Show Gist options
  • Save eneajaho/a4fef79de09941d9aa6b0349e6c684ba to your computer and use it in GitHub Desktop.
Save eneajaho/a4fef79de09941d9aa6b0349e6c684ba to your computer and use it in GitHub Desktop.
Vendure Admin Regenerate cache button
```ts
import { NgModule } from '@angular/core';
import { SharedModule, addActionBarItem } from '@vendure/admin-ui/core';
import { take } from 'rxjs/operators';
@NgModule({
imports: [ SharedModule ],
providers: [
addActionBarItem({
id: 'isr-regenerate-cache',
label: 'Regenerate cache',
locationId: 'product-detail',
buttonStyle: 'outline',
onClick: (event, ctx) => {
const productId = ctx.route.snapshot.params.id;
const product$ = ctx.dataService.product.getProduct(productId).single$;
product$.pipe(take(1)).subscribe(res => {
const clientAppPath = `http://localhost:4200/api/invalidate?secret=MY_TOKEN&urlToInvalidate=`;
const cachePath = `/product/${res.product?.slug}`;
fetch(clientAppPath + cachePath).then(r => r.json()).then(res => {
console.log(res);
alert(res.message)
});
})}
}),
]
})
export class ISRClearCacheModule {}
```
### Usage in vendure-config.ts
```ts
AdminUiPlugin.init({
route: 'admin',
port: 3002,
app: compileUiExtensions({
outputPath: path.join(__dirname, '../admin-ui'),
extensions: [{
extensionPath: path.join(__dirname, 'ui-extensions'),
ngModules: [{
type: 'shared',
ngModuleFileName: 'regenerate-cache.module.ts',
ngModuleName: 'ISRClearCacheModule',
}],
}],
}),
}),
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment