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

Support prerendered apps #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

evoactivity
Copy link

What Changed & Why

  • Uses a glob pattern to find all index.html files
  • Loops over list and uploads each file to redis
  • Uses the directory structure as part of the redis key

Related issues

Fixes #132

PR Checklist

  • Add tests
  • Add documentation

@lukemelia

@evoactivity evoactivity changed the title Support prerenderred apps Support prerendered apps Sep 1, 2023
@evoactivity
Copy link
Author

evoactivity commented Sep 26, 2023

I've been using this successfully with this code to fetch the correct page

import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
import type { RequestContract } from '@ioc:Adonis/Core/Request';
import Redis from '@ioc:Adonis/Addons/Redis';

/**
 * Computes the key based on request's query string.
 * Defaults to the current key from Redis if not provided in the query string.
 */
async function computeKey(request: RequestContract): Promise<string> {
  const { revision } = request.qs();
  return revision || (await Redis.get(`web-app:index:current`));
}

/**
 * Normalizes the URL for consistent lookup.
 * Strips leading and trailing slashes and appends ':index' as a suffix.
 */
function normalizeUrl(url: string): string {
  const strippedUrl = url.replace(/^\/|\/$/g, '');
  return strippedUrl === '' ? 'index' : `${strippedUrl}:index`;
}

export default class EmberWebController {
  public async index({ request, response }: HttpContextContract) {
    const key = await computeKey(request);
    const normalizedUrl = normalizeUrl(request.url());

    // Attempt to retrieve the HTML for the specified URL and key.
    const htmlKey = `web-app:${normalizedUrl}:${key}`;
    if (await Redis.exists(htmlKey)) {
      const html = await Redis.get(htmlKey);
      return response.ok(html);
    }

    // Fallback to retrieve the default HTML for the key.
    const defaultHtml = await Redis.get(`web-app:index:${key}`);
    return response.ok(defaultHtml);
  }
}

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.

Prember / Multiple File Support
1 participant