https://discord.cloudflare.com logo
Join Discord
Powered by
# workers-discussions
  • z

    zszszsz

    02/12/2023, 11:57 AM
    Then why would you ever need a scheduled worker to do the updates ?
  • s

    sirhype

    02/12/2023, 12:01 PM
    You make a very good point lol
  • d

    Deleted User

    02/12/2023, 12:01 PM
    Copy code
    js
    let cacheKey = `https://${url.host}${url.pathname}`;
    let data = await caches.default.match(cacheKey);
    
    if (data) {
        data = await data.json();
        return Response.json({ data: data.data, });
    }
    
    
    let catResponse = await fetch('https://cataas.com/cat?json=true');
    catResponse = await catResponse.json();
    
    let cachedAPILayer = Response.json({ data: catResponse });
    
    cachedAPILayer.headers.set("Cache-Control", `s-maxage=5`);
    ctx.waitUntil(caches.default.put(cacheKey, cachedAPILayer.clone()));
    
    return cachedAPILayer;
    caches random api i found for 5 sec @ https://test.svge.ms/cache
  • d

    Deleted User

    02/12/2023, 12:02 PM
    lots of reused code so variables are not clean lol
  • s

    sirhype

    02/12/2023, 12:04 PM
    Appreciated! I'll sleep on it and figure something out
  • s

    sirhype

    02/12/2023, 12:04 PM
    Thank you both for your help 🙏
  • z

    zszszsz

    02/12/2023, 12:07 PM
    s-maxage=5 hmm
  • z

    zszszsz

    02/12/2023, 12:09 PM
    You can make it longer if you need, maybe make it 86400 if you want. And you can choose to cache upstream response directly, or to cache "your" own generated response. You can cache the response right before you return.
  • z

    zszszsz

    02/12/2023, 12:10 PM
    Cache API has 2 bad parts:
  • z

    zszszsz

    02/12/2023, 12:10 PM
    1. A cache entry may be deleted before expiration for no reason.
  • z

    zszszsz

    02/12/2023, 12:11 PM
    2. Cache operations are count as outgoing sub requests
  • s

    Salamus

    02/12/2023, 1:10 PM
    Hey! if i make a subdomain. abc.mydomain, and from workers i want to fetch abc.mydomain:1234, will it work workers will be for example on xyz.mydomain
  • i

    itsmatteomanf

    02/12/2023, 1:11 PM
    It will go direct to origin, provided the Worker runs on your domain and, in the case of Custom Domains (and not Routes), runs on the specific subdomain.
  • s

    Salamus

    02/12/2023, 1:11 PM
    so it will work?
  • s

    Salamus

    02/12/2023, 1:11 PM
    oh, specific subdomain?
  • s

    Skye

    02/12/2023, 1:12 PM
  • i

    itsmatteomanf

    02/12/2023, 1:12 PM
    If the Worker is setup with a Custom Domain, it needs to be the on the actual domain as far as I understand it.
  • s

    Salamus

    02/12/2023, 1:12 PM
    what is a zone @Skye
  • s

    Salamus

    02/12/2023, 1:12 PM
    is it a domain?
  • s

    Salamus

    02/12/2023, 1:12 PM
    or subdomain
  • s

    Skye

    02/12/2023, 1:13 PM
    I believe in this case subdomain
  • s

    Salamus

    02/12/2023, 1:13 PM
    😢
  • i

    itsmatteomanf

    02/12/2023, 1:13 PM
    If it's set-up as a custom domain, yes.
  • p

    Probert

    02/12/2023, 5:39 PM
    Is it possible to run Puppeteer or Playwright inside a worker using something like Browserless? Like does it run? Or do those packages use node api's regardless?
  • h

    HardAtWork

    02/12/2023, 5:41 PM
    Coming : https://blog.cloudflare.com/introducing-workers-browser-rendering-api/
  • p

    Probert

    02/12/2023, 5:42 PM
    I saw that and that is very cool! But for now could I do this in theory with Browserless?
  • h

    HardAtWork

    02/12/2023, 5:44 PM
    No. AFAIK,
    browserless
    relies on Headless Chromium, which while it might work in Workers(if compiled to WebAssembly), has the distinct disadvantage of being too large for the 1-5 MB limit for a deployed script.
  • h

    HardAtWork

    02/12/2023, 5:44 PM
    What are you trying to render, if I may ask?
  • p

    Probert

    02/12/2023, 5:46 PM
    Browserless runs the browser in their environment, right? And I would use the playwright package to connect to that browser? Not sure, that's why I came here to ask 😅
  • p

    Probert

    02/12/2023, 5:47 PM
    I'm trying to render social images for now, maybe more stuff later
1...226522662267...2509Latest