Orbit

एज फंक्शंस

Edge functions are small JavaScript handlers that run on Kapsule's edge network before a request reaches your project, so you can do redirects, header injection, A/B routing and bot filtering…

Edge functions छोटे JavaScript हैंडलर हैं जो Kapsule के edge नेटवर्क पर आपके प्रोजेक्ट तक पहुँचने से पहले चलते हैं, इसलिए आप अपने ऐप के लिए राउंड ट्रिप के बिना redirects, header injection, A/B routing और bot filtering कर सकते हैं। यह गाइड एक लिखने, स्वीकृत entry-point फॉर्म, path matching, कई फंक्शन कैसे interact करते हैं, deploying, और फंक्शन throw करने पर क्या होता है, इसे कवर करता है।

जहाँ वे रहते हैं

Orbit में अपना प्रोजेक्ट खोलें और Edge functions टैब पर क्लिक करें। प्रत्येक फंक्शन एक प्रोजेक्ट के अंतर्गत आता है और इसकी status badge के साथ सूचीबद्ध है: LIVE, PAUSED, NOT DEPLOYED या DEPLOY FAILED

Orbit प्रोजेक्ट का Edge functions टैब

फंक्शन बनाना

  1. New function पर क्लिक करें।
  2. Name भरें (120 characters तक)।
  3. Path pattern भरें, URL पैटर्न जिस पर यह फंक्शन चलना चाहिए।
  4. एक Trigger चुनें: Request (origin से पहले), Response (origin के बाद), या Both।
  5. एक Environment scope चुनें: All environments, Production only, या Preview only।
  6. Function body में हैंडलर लिखें।
  7. Save पर क्लिक करें, फिर Deploy to edge

request object हमेशा scope में है, और source 64 KB तक capped है।

हैंडलर लिखना

Edge पर request का उत्तर देने के लिए Response return करें। कुछ भी return न करें, या undefined, request को बिना बदलाव के अपने प्रोजेक्ट के माध्यम से pass करने के लिए।

export default {
  async fetch(request) {
    const url = new URL(request.url)

    // Redirect /old to /new
    if (url.pathname === '/old') {
      return new Response(null, {
        status: 301,
        headers: { Location: '/new' },
      })
    }

    // Returning nothing passes through to the origin
  },
}

Named-function फॉर्म भी काम करता है:

export default async function handler(request) {
  // Add a security header to every response
  const response = await fetch(request)
  const headers = new Headers(response.headers)
  headers.set('X-Frame-Options', 'DENY')
  return new Response(response.body, { status: response.status, headers })
}

समर्थित Entry-Point फॉर्म

फॉर्मउदाहरण
fetch method के साथ objectexport default { async fetch(request) { ... } }
Arrow fetch property के साथ objectexport default { fetch: async (request) => { ... } }
Named function declarationexport default async function handler(request) { ... }
Bare body, कोई export नहींstatements को सीधे लिखें, कोई wrapper नहीं

कोई भी अन्य चीज़ जो export default का उपयोग करती है, जैसे export default class, Save पर क्लिक करने पर अस्वीकार कर दी जाती है, एक error के साथ जो दो फॉर्म को नाम देता है। Validation save time पर चलता है न कि deploy time पर, इसलिए आप तुरंत पता चल जाता है और एक broken फंक्शन कभी publish नहीं होता है।

पाथ पैटर्न

Path pattern तय करता है कि कौन से requests फंक्शन को चलाते हैं। इसे / से शुरू होना चाहिए और 2048 characters तक हो सकता है। * characters की किसी भी run को match करता है और ? एक single character को match करता है। एक pattern जिसमें कोई wildcard नहीं है, उस exact path और इसके नीचे सब कुछ match करता है।

पैटर्नMatch करता है
/*हर path
/api/*/api/ से शुरू होने वाली कोई भी चीज़
/blog/*/commentsउदाहरण के लिए /blog/my-post/comments
/page/page, और /page/ के नीचे कोई भी चीज़

request Object

request एक मानक Fetch API Request है। आप URL, method, headers और body को पढ़ सकते हैं:

export default {
  async fetch(request) {
    const url    = new URL(request.url)
    const cookie = request.headers.get('cookie') ?? ''
    const ua     = request.headers.get('user-agent') ?? ''

    if (ua.includes('BadBot')) {
      return new Response('Forbidden', { status: 403 })
    }
  },
}

यह मत मानें कि एक header मौजूद है क्योंकि आपने इसे किसी अन्य platform पर देखा है। अपने own फंक्शन को read करें जो actually receives करता है (फंक्शन से log करें, या throwaway path पर एक debug response में उन्हें return करें) इससे पहले कि आप एक पर branch करें। एक हैंडलर जो एक header पर branch करता है जो कभी present नहीं है, हर request पर silently गलत path लेता है।

सामान्य पैटर्न

पुराने URLs को Redirect करें

export default {
  async fetch(request) {
    const url = new URL(request.url)
    const redirects = {
      '/old-about':   '/about',
      '/old-contact': '/contact',
    }
    const dest = redirects[url.pathname]
    if (dest) return Response.redirect(url.origin + dest, 301)
  },
}

कुछ straightforward path-to-path moves के लिए, इसके बजाय built-in redirect engine का उपयोग करें: इसे कोई code की ज़रूरत नहीं है और यह Settings में configured है। Configuring Redirects and Rewrites देखें।

सुरक्षा Headers जोड़ें

export default async function handler(request) {
  const response = await fetch(request)
  const headers = new Headers(response.headers)
  headers.set('X-Frame-Options', 'DENY')
  headers.set('X-Content-Type-Options', 'nosniff')
  headers.set('Referrer-Policy', 'strict-origin-when-cross-origin')
  return new Response(response.body, {
    status: response.status,
    statusText: response.statusText,
    headers,
  })
}

Static header rules के लिए आपको code की ज़रूरत नहीं है। Settings में एक response headers section है जिसमें HSTS, CSP, no-embed, no-sniff, referrer policy और CORS के लिए quick-add presets हैं।

A/B Routing

export default {
  async fetch(request) {
    const url     = new URL(request.url)
    const variant = Math.random() < 0.5 ? 'a' : 'b'
    url.searchParams.set('variant', variant)
    return fetch(url.toString(), request)
  },
}

Environment Scope

Scopeचलता है
All environmentsProduction, staging और हर branch preview
Production onlyProduction environment
Preview onlyहर non-production environment

एक नया फंक्शन Preview only के रूप में ship करें पहले, confirm करें कि यह एक branch preview पर behave करता है, फिर इसे All environments में switch करें। एक edge फंक्शन हर request के सामने चलता है, इसलिए एक में mistake एक बार में हर page पर एक mistake है।

कई Functions कैसे Interact करते हैं

आपके प्रोजेक्ट के सभी enabled फंक्शन उन्हें बनाए गए order में evaluate किए जाते हैं। हर request के लिए, edge list पर चलता है और पहला फंक्शन चलाता है जिसका path pattern request path को match करता है और जिसका scope environment को cover करता है।

  • यदि वह फंक्शन Response return करता है, तो यह भेजा जाता है और evaluation रुक जाता है।
  • यदि यह कुछ भी return नहीं करता है, तो evaluation अगले matching फंक्शन में जारी रहता है।
  • यदि उनमें से कोई भी Response return नहीं करता है, तो request आपके प्रोजेक्ट में सामान्य तरीके से जाता है।

इसका मतलब है कि एक broad /* फंक्शन जो जल्दी बनाया गया है, एक narrower को shadow कर सकता है जो बाद में बनाया गया है, यदि broad एक Response return करता है। specific ones को पहले बनाएं, या broad one को उन paths के लिए कुछ भी return न करने दें जिन्हें यह handle नहीं करना चाहिए।

Deploy करना

Deploy to edge पर क्लिक करें। Deploy पूरे edge network के लिए database की current state से एक single combined router को regenerate करता है, इसलिए किसी भी फंक्शन को enable, disable, edit या delete करने से सब कुछ republish होता है। Changes आमतौर पर कुछ सेकंड में प्रभावी हो जाते हैं।

प्रत्येक फंक्शन row एक Deploy log रखता है जो last deploy के steps दिखाता है, और एक DEPLOY FAILED badge error के साथ यदि यह सफल नहीं हुआ।

Pause एक फंक्शन को router से बाहर निकालता है इसे delete किए बिना, जो एक misbehaving फंक्शन को back out करने का सबसे तेज़ तरीका है। Resume इसे वापस रखता है।

जब एक Function Throw करता है

एक फंक्शन के अंदर एक exception edge पर catch किया जाता है। Error को log किया जाता है और request आपके प्रोजेक्ट में fall through करता है जैसे फंक्शन कुछ भी return नहीं किया था।

यह एक safety net है, एक monitoring system नहीं। एक फंक्शन जो हर request पर throw करता है, आपके visitors के point of view से silently fail करता है, और आपका traffic simply ऐसा behave करता है जैसे कि फंक्शन exist नहीं करता है। यदि एक फंक्शन अपना intended effect रखना बंद कर देता है, तो routing को suspect करने से पहले एक exception को suspect करें।

सीमाएँ

  • प्रोजेक्ट के अनुसार 20 functions तक। 21st अस्वीकार कर दिया जाता है।
  • प्रत्येक फंक्शन के लिए 64 KB तक source।
  • Name के लिए 120 characters तक, और path pattern के लिए 2048 characters तक।

संबंधित पठन

क्या आपको अभी भी सहायता की आवश्यकता है?

हमें ईमेल करें support@kapsulehost.com या KPanel में चैट खोलें।

KPanel खोलें