apptvty 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -468,7 +468,9 @@ var KNOWN_CRAWLERS = [
468
468
  // Allen Institute
469
469
  { name: "AI2Bot", organization: "Allen Institute for AI", patterns: [/AI2Bot/i] },
470
470
  // Mistral
471
- { name: "MistralBot", organization: "Mistral AI", patterns: [/MistralBot/i] }
471
+ { name: "MistralBot", organization: "Mistral AI", patterns: [/MistralBot/i] },
472
+ // Cloudflare
473
+ { name: "Cloudflare-BrowserRendering", organization: "Cloudflare", patterns: [/CloudflareBrowserRenderingCrawler/i] }
472
474
  ];
473
475
  var AI_PATTERN_MATCHES = [
474
476
  [/openai/i, 0.9],
@@ -1279,56 +1281,59 @@ function withApptvty(config, next) {
1279
1281
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
1280
1282
  }).catch(() => {
1281
1283
  });
1282
- if (isAi || scraperService.isScraperService) {
1284
+ if (isScraper) {
1283
1285
  const proxyReq = new Request(request.url, { headers: new Headers(request.headers) });
1284
1286
  proxyReq.headers.set("x-apptvty-internal", "true");
1285
1287
  const res = await fetch(proxyReq);
1286
1288
  const contentType = res.headers.get("content-type") ?? "";
1287
1289
  if (contentType.includes("text/html")) {
1288
1290
  const html = await res.text();
1289
- let markdown = convertHtmlToMarkdown(html);
1290
- markdown += `
1291
+ if (isAi || scraperService.isScraperService) {
1292
+ let markdown = convertHtmlToMarkdown(html);
1293
+ markdown += `
1291
1294
 
1292
1295
  ---
1293
1296
  > **Sponsored:** [${ad.text}](${ad.url}) - ${ad.advertiser}
1294
1297
  `;
1295
- return new import_server.NextResponse(markdown, {
1296
- status: res.status,
1297
- headers: {
1298
- "Content-Type": "text/markdown",
1299
- "X-Apptvty-AEO": "true",
1300
- "X-Sponsored-Content": `${ad.text}; url=${ad.url}`
1301
- }
1302
- });
1303
- }
1304
- }
1305
- const originalHeaders = headersToRecord(response.headers);
1306
- if (originalHeaders["content-type"]?.includes("text/html")) {
1307
- const html = await response.text();
1308
- const jsonLd = `
1298
+ return new import_server.NextResponse(markdown, {
1299
+ status: res.status,
1300
+ headers: {
1301
+ "Content-Type": "text/markdown",
1302
+ "X-Apptvty-AEO": "true",
1303
+ "X-Sponsored-Content": `${ad.text}; url=${ad.url}`
1304
+ }
1305
+ });
1306
+ }
1307
+ const discoveryUrl2 = `${request.nextUrl.origin}${queryPath}?q=${encodeURIComponent(pathname)}`;
1308
+ const jsonLd = `
1309
1309
  <script type="application/ld+json">{"@context":"https://schema.org","@type":"CreativeWork","author":{"@type":"Organization","name":"${ad.advertiser}"},"mainEntityOfPage":{"@type":"WebPage","@id":"${ad.url}"},"headline":"Sponsored: ${ad.text}"}</script>
1310
1310
  `;
1311
- const stealthDiv = `
1311
+ const discoveryMeta = `<meta name="apptvty-aeo-discovery" content="${discoveryUrl2}"><link rel="alternate" type="application/json" href="${discoveryUrl2}">`;
1312
+ const stealthDiv = `
1312
1313
  <div style="display:none !important;visibility:hidden;height:0;width:0;overflow:hidden;" aria-hidden="true" data-apptvty-ad="${ad.impression_id}">Sponsored by ${ad.advertiser}: <a href="${ad.url}">${ad.text}</a></div>
1313
1314
  `;
1314
- let modifiedHtml = html;
1315
- if (html.includes("</head>")) {
1316
- modifiedHtml = html.replace("</head>", `${jsonLd}</head>`);
1317
- }
1318
- if (modifiedHtml.includes("</body>")) {
1319
- modifiedHtml = modifiedHtml.replace("</body>", `${stealthDiv}</body>`);
1320
- } else {
1321
- modifiedHtml += stealthDiv;
1322
- }
1323
- return new import_server.NextResponse(modifiedHtml, {
1324
- status: response.status,
1325
- headers: {
1326
- ...originalHeaders,
1327
- "X-Sponsored-Content": `${ad.text}; url=${ad.url}`
1315
+ let modifiedHtml = html;
1316
+ if (html.includes("</head>")) {
1317
+ modifiedHtml = html.replace("</head>", `${jsonLd}${discoveryMeta}</head>`);
1328
1318
  }
1329
- });
1319
+ if (modifiedHtml.includes("</body>")) {
1320
+ modifiedHtml = modifiedHtml.replace("</body>", `${stealthDiv}</body>`);
1321
+ } else {
1322
+ modifiedHtml += stealthDiv;
1323
+ }
1324
+ return new import_server.NextResponse(modifiedHtml, {
1325
+ status: res.status,
1326
+ headers: {
1327
+ ...headersToRecord(res.headers),
1328
+ "X-Sponsored-Content": `${ad.text}; url=${ad.url}`,
1329
+ "Link": `<${discoveryUrl2}>; rel="alternate"; type="application/json"; title="Machine-readable content"`
1330
+ }
1331
+ });
1332
+ }
1330
1333
  }
1334
+ const discoveryUrl = `${request.nextUrl.origin}${queryPath}?q=${encodeURIComponent(pathname)}`;
1331
1335
  response.headers.set("X-Sponsored-Content", `${ad.text}; url=${ad.url}`);
1336
+ response.headers.set("Link", `<${discoveryUrl}>; rel="alternate"; type="application/json"`);
1332
1337
  }
1333
1338
  } catch (err) {
1334
1339
  if (config.debug) console.warn("[apptvty] Stealth injection failed:", err);