Compare commits
2
Commits
a9c57d4985
...
a1d2f9db67
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a1d2f9db67 | ||
|
|
d1a4a84d24 |
@@ -0,0 +1,44 @@
|
|||||||
|
import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework";
|
||||||
|
import { ContainerRegistrationKeys } from "@medusajs/framework/utils";
|
||||||
|
|
||||||
|
export default async function revalidateStorefrontHandler({
|
||||||
|
container,
|
||||||
|
}: SubscriberArgs) {
|
||||||
|
const logger = container.resolve(ContainerRegistrationKeys.LOGGER);
|
||||||
|
|
||||||
|
const storefrontUrl = process.env.STOREFRONT_URL;
|
||||||
|
const secret = process.env.REVALIDATE_SECRET;
|
||||||
|
|
||||||
|
if (!storefrontUrl || !secret) {
|
||||||
|
logger.warn(
|
||||||
|
"STOREFRONT_URL o REVALIDATE_SECRET no configurados; se omite la revalidación del storefront.",
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${storefrontUrl}/api/revalidate`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "x-revalidate-secret": secret },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
logger.warn(
|
||||||
|
`Revalidación del storefront respondió HTTP ${response.status}.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn(
|
||||||
|
`No se pudo revalidar el storefront: ${(error as Error).message}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const config: SubscriberConfig = {
|
||||||
|
event: [
|
||||||
|
"product.updated",
|
||||||
|
"product-variant.updated",
|
||||||
|
"product-variant.created",
|
||||||
|
"product-variant.deleted",
|
||||||
|
],
|
||||||
|
};
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { revalidateTag } from "next/cache";
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
const secret = request.headers.get("x-revalidate-secret");
|
||||||
|
|
||||||
|
if (!secret || secret !== process.env.REVALIDATE_SECRET) {
|
||||||
|
return Response.json({ message: "Invalid secret" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
revalidateTag("medusa-products");
|
||||||
|
|
||||||
|
return Response.json({ revalidated: true, tag: "medusa-products" }, { status: 200 });
|
||||||
|
}
|
||||||
@@ -22,6 +22,11 @@ stringData:
|
|||||||
|
|
||||||
JWT_SECRET: CAMBIAR
|
JWT_SECRET: CAMBIAR
|
||||||
COOKIE_SECRET: CAMBIAR
|
COOKIE_SECRET: CAMBIAR
|
||||||
|
|
||||||
|
# Debe ser el MISMO valor que REVALIDATE_SECRET en el Secret
|
||||||
|
# commerce-storefront: el subscriber de Medusa lo envía como header
|
||||||
|
# x-revalidate-secret al invocar POST /api/revalidate en el storefront.
|
||||||
|
REVALIDATE_SECRET: CAMBIAR
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Secret
|
kind: Secret
|
||||||
@@ -30,3 +35,6 @@ metadata:
|
|||||||
type: Opaque
|
type: Opaque
|
||||||
stringData:
|
stringData:
|
||||||
MEDUSA_PUBLISHABLE_KEY: pk_CAMBIAR_DESPUES_DE_CREARLA_EN_MEDUSA
|
MEDUSA_PUBLISHABLE_KEY: pk_CAMBIAR_DESPUES_DE_CREARLA_EN_MEDUSA
|
||||||
|
|
||||||
|
# Mismo valor que REVALIDATE_SECRET en commerce-secrets.
|
||||||
|
REVALIDATE_SECRET: CAMBIAR
|
||||||
|
|||||||
Reference in New Issue
Block a user