Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added nextjs 13 app router native support & docs improvements. #1141

Merged
merged 12 commits into from
Jun 20, 2023

Conversation

KevinEdry
Copy link
Contributor

@KevinEdry KevinEdry commented Jun 19, 2023

Summary

This PR aims to revise and add two main things:

Nextjs 13 App Router

Because Nextjs 13 now expects a named method function as a route handler (POST, GET etc..), we need to provide it as part of the Queue and CronJob methods, we also want the queue instances themselves, so I've made it so it would be accessible via object destructuring, that way we can export both POST and the queue instance in one go, and alias the queue instance with a meaningful name.

Queue Usage:

  export const { POST, queue: sampleQueue } = Queue(
   "/api/queues/sample",
   (payload) => {
       ...logic goes here
  });
 
 await sampleQueue.enqueue({ jobData });

CronJob Usage:

  export const { POST } = CronJob(
   "/api/queues/sample",
   "0 0 * * 1"
   (payload) => {
       ...logic goes here
  });

I've added some docs changes as well to explain the usage of the new queue api.

The object destructuring is the best way I could think of to keep the named export function POST and still have a meaningful name for the queue instance, if you can think of a better implementation for this please let me know!

Changelog:

code

  • added new next13.ts file to support nextjs 13 app directory natively.

docs

  • added to proper documentation for all of the Nextjs changes.
  • added new "Development" category which explains how to run the Quirrel instance in a docker compose, or as a standalone instance.
  • added a new and improved "Deployment" category with several guides on how to deploy quirrel (docker, railway, fly and vercel).
  • removed the old "Deploy" page.

BEGIN_COMMIT_OVERRIDE
feat: added nextjs 13 app router native support & docs improvements
END_COMMIT_OVERRIDE

@netlify
Copy link

netlify bot commented Jun 19, 2023

Deploy Preview for quirrel-docs canceled.

Name Link
🔨 Latest commit f46f66e
🔍 Latest deploy log https://app.netlify.com/sites/quirrel-docs/deploys/6491c0282aad4300086b488c

@netlify
Copy link

netlify bot commented Jun 19, 2023

👷 Deploy request for quirrel-development-ui pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit f46f66e

@KevinEdry KevinEdry changed the title added nextjs 13 app router native support added nextjs 13 app router native support & docs improvements. Jun 20, 2023
@Skn0tt
Copy link
Member

Skn0tt commented Jun 20, 2023

Hi @KevinEdry, this is an amazing PR! Thank you so much - I love how you improved the docs. It's quite big, so reviewing will take a couple iterations, but we'll get there.

@coveralls
Copy link

coveralls commented Jun 20, 2023

Coverage Status

coverage: 82.464%. remained the same when pulling e2f0146 on KevinEdry:main into 9344abf on quirrel-dev:main.

src/next-pages.ts Dismissed Show dismissed Hide dismissed
@Skn0tt
Copy link
Member

Skn0tt commented Jun 20, 2023

I hope it's okay that i'm committing onto your branch! I figured that's the fastest way to make progress. I'll comment about the changes i'm making and the rationale behind it, feel free to leave comments about anything.

In 5b430d7, i've reworked the quirrel/next13 implementation to be based on the others we have in that directory. I've also changed the user-facing signature to be in-line with how quirre/sveltekit works: https://docs.quirrel.dev/api/sveltekit This means that devs won't destructure the return value of Queue, but instead they'll export it twice.
I've also renamed the file to quirrel/next-app, since next13 will probably grow out-of-date pretty soon :D

@Skn0tt
Copy link
Member

Skn0tt commented Jun 20, 2023

I've reverted the changes to the CronJob API docs. I don't think we need to differentiate between Next.js App Router / Pages Router on that page - it's not framework-specific, and Next.js isn't the only framework that's supported by Quirrel.

@KevinEdry
Copy link
Contributor Author

KevinEdry commented Jun 20, 2023

It is totally fine for you to commit on my branch, I didn't notice you had a similar implementation in quirre/sveltekit, I would probably would go with that.
For the CronJob, I agree that this isn't framework specific, but I do think the docs should also reflect the implementation for every framework, such as a tab selector for each framework and it's implementation. (I might do that later).

*
* export const POST = sampleCron;
*/
export function CronJob(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add here a <Payload> generic and pass it to the handler or the Queue.

export function CronJob<Payload>(
  route: string,
  schedule: string,
  handler: QuirrelJobHandler<Payload>,
  options?: QuirrelOptions<Payload>
) {
  return Queue(route, handler, options);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CronJobs don't receive any kind of payload, so there's no need for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use a cronjob with a payload, the cron is starting the job, the job is making an api request, and if the response contains a cursor, it invokes the same job with the cursor:

export const { POST, queue: companiesQueue } = CronJob<Payload>(
  "/api/queue/companies",
  "0 0 * * 1",
  async (payload?: Payload) => {
    try {
      const fetchUrl =
        payload?.cursor != null
          ? payload.cursor
          : "https://api.random-example.com/something";

      const res = await fetchApi<Ticker>(fetchUrl);
      const { results, next_url } = tickersSchema.parse(res);

      if (next_url != null) {
        companiesQueue.enqueue({ cursor: next_url });
      }

      return;
    } catch (error) {
      console.log({ error });
      throw error;
    }
  }
);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! I'd have never imagined anyone to use a CronJob like this 😁 I think if we documented this, it'd be very non-obvious how it works in most cases. So i'd prefer to not document it. I'm sure you'll find a workaround for your specific case :D

@Skn0tt
Copy link
Member

Skn0tt commented Jun 20, 2023

in 1d51c94, i've removed the docker-compose development docs. Quirrel has a local dev mode, and that should be used locally. I think that docker-compose is way too complex for most usecases of this, so I don't want to highlight it in the docs. Appreciate the document, though!

@KevinEdry
Copy link
Contributor Author

@Skn0tt I wonder if we also need to change the cron-detector since we are no longer importing from quirrel/next.

@Skn0tt
Copy link
Member

Skn0tt commented Jun 20, 2023

but I do think the docs should also reflect the implementation for every framework

I want to keep the documentation small and concise, and i'm unsure if another documentation for CronJob for every single framework adds much value.

@Skn0tt
Copy link
Member

Skn0tt commented Jun 20, 2023

I wonder if we also need to change the cron-detector

I don't think so, the cron-detector isn't hard-coded on framework names, but matches on quirrel/*:

const quirrelImport = /["']quirrel\/(.*)["']/.exec(file);

@Skn0tt
Copy link
Member

Skn0tt commented Jun 20, 2023

This is now in a state where i'm happy with it. @KevinEdry could you give it another read and let me know if there's anything you think needs changing?

@KevinEdry
Copy link
Contributor Author

KevinEdry commented Jun 20, 2023

@Skn0tt LGTM, the only thing I would change is to add the Payload to the CronJob, but it is your call, I will manage either way in my project and my specific use-case.
I hope these changes will help people use this lib more, I found it really useful for my purposes.

@Skn0tt Skn0tt merged commit 91a3d93 into quirrel-dev:main Jun 20, 2023
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants