Skip to content

Tagged source #17

@fabiancook

Description

@fabiancook

This is related to full picture, not to current state

A callback style source was suggested here: comment link

With a rebuttal that it wouldn't work with a function reference as the install event scope may no longer be accessible when the route is used: comment link

The reasoning for not having a function reference makes sense as a source, but being able to use either a tag to shortcut re-routing inside the fetch handler, or providing a function name to use as a name for an exported function from a service worker would be helpful.

Tag functionality would be similar to the sync API.

I've also included an alternative where a function source type could be added, but I feel like its over reaching compared to tagging.

Example

addEventListener("install", event => {
 event.addRoutes([
   {
     condition: [
       { requestMethod: "get" },
       { urlPattern: { pathname: "/api/users" }
     ],
     source: [
       {
         type: "cache"
       },
       {
         type: "fetch-event",
         // Addition to the fetch-event or race-network-and-fetch-handler sources
         tag: "listUsers"
       }
     ]
   },
   {
     condition: [
       { requestMethod: "post" },
       { urlPattern: { pathname: "/api/users" }
     ],
     source: [
       {
         type: "fetch-event",
         tag: "createUser"
       }
     ]
   }
 ])
})

const handlers = {
  listUsers,
  createUser
}

addEventListener("fetch", event => {
  if (event.tag === "listUsers") {
    event.respondWith(listUsers(event));
  } else if (event.tag === "createUser") {
    event.respondWith(createUser(event));
  }
});
Alternatives

Named Export

addEventListener("install", event => {
 event.addRoutes([
   {
     condition: [
       { requestMethod: "get" },
       { urlPattern: { pathname: "/api/users" }
     ],
     source: [
       {
         type: "cache"
       },
       {
         type: "function",
         name: "listUsers"
       }
     ]
   },
   {
     condition: [
       { requestMethod: "post" },
       { urlPattern: { pathname: "/api/users" }
     ],
     source: [
       {
         type: "function",
         name: "createUser"
       }
     ]
   }
 ])
})

export {
  listUsers,
  createUser
}

Mixed Functionality

addEventListener("install", event => {
 event.addRoutes([
   {
     condition: [
       { requestMethod: "get" },
       { urlPattern: { pathname: "/api/users" }
     ],
     source: [
       {
         type: "cache"
       },
       {
         type: "function",
         name: "fetch",
         tag: "listUsers"
       }
     ]
   },
   {
     condition: [
       { requestMethod: "post" },
       { urlPattern: { pathname: "/api/users" }
     ],
     source: [
       {
         type: "function",
         name: "fetch",
         tag: "createUser"
       }
     ]
   }
 ])
})

export function fetch(event) {
  if (event.tag === "listUsers") {
    return listUsers(event);
  } else if (event.tag === "createUser") {
    return createUser(event);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions