Repository: denoland/fresh
File: src/plugins/fs_routes/mod.ts (TypeScript)

Code before:

    // Remove any elements not matching our parent path anymore
    const middlewares: MiddlewareFn<State>[] = [];
    let components: AnyComponent<PageProps<unknown, State>>[] = [];

    let skipApp = !!routeMod.config?.skipAppWrapper;
    const skipLayouts = !!routeMod.config?.skipInheritedLayouts;

    for (let k = 0; k < stack.length; k++) {
      const mod = stack[k];
      if (mod.path.endsWith("/_middleware")) {
        if (mod.handlers !== null && !isHandlerByMethod(mod.handlers)) {
          middlewares.push(mod.handlers as MiddlewareFn<State>);
        } else if (Array.isArray(mod.handlers)) {
          middlewares.push(...mod.handlers);
        }
      }

      // _app template
      if (skipApp && mod.path === "/_app") {
        hasApp = false;
        continue;
      } else if (!skipApp && mod.config?.skipAppWrapper) {
        skipApp = true;
        if (hasApp) {
          hasApp = false;
          // _app component is always first
          components.shift();
        }
      }

      // _layouts
      if (skipLayouts && mod.path.endsWith("/_layout")) {
        continue;
      } else if (!skipLayouts && mod.config?.skipInheritedLayouts) {
        const first = components.length > 0 ? components[0] : null;
        components = [];

        if (!skipApp && hasApp && first !== null) {
          components.push(first);
        }
      }

      if (mod.path.endsWith("/_error") || mod.path.endsWith("/_500")) {
        const handlers = mod.handlers;
        const handler = handlers === null ||
            (isHandlerByMethod(handlers) && Object.keys(handlers).length === 0)
          ? undefined
          : typeof handlers === "function"
          ? handlers
          : undefined; // FIXME: Method handler

Review comment (bug):
I don't know if it's correct just to remove these lines. It looks wrong to me to set `hasApp` to `false` if we're looping over every route, since a latter route may have the app wrapper disabled always.

This change makes the test I added pass anyway...

Code after:

    // Remove any elements not matching our parent path anymore
    const middlewares: MiddlewareFn<State>[] = [];
    let components: AnyComponent<PageProps<unknown, State>>[] = [];

    let skipApp = !!routeMod.config?.skipAppWrapper;
    const skipLayouts = !!routeMod.config?.skipInheritedLayouts;

    for (let k = 0; k < stack.length; k++) {
      const mod = stack[k];
      if (mod.path.endsWith("/_middleware")) {
        if (mod.handlers !== null && !isHandlerByMethod(mod.handlers)) {
          middlewares.push(mod.handlers as MiddlewareFn<State>);
        } else if (Array.isArray(mod.handlers)) {
          middlewares.push(...mod.handlers);
        }
      }

      // _app template
      if (skipApp && mod.path === "/_app") {
        continue;
      } else if (!skipApp && mod.config?.skipAppWrapper) {
        skipApp = true;
        if (hasApp) {
          // _app component is always first
          components.shift();
        }
      }

      // _layouts
      if (skipLayouts && mod.path.endsWith("/_layout")) {
        continue;
      } else if (!skipLayouts && mod.config?.skipInheritedLayouts) {
        const first = components.length > 0 ? components[0] : null;
        components = [];

        if (!skipApp && hasApp && first !== null) {
          components.push(first);
        }
      }

      if (mod.path.endsWith("/_error") || mod.path.endsWith("/_500")) {
        const handlers = mod.handlers;
        const handler = handlers === null ||
            (isHandlerByMethod(handlers) && Object.keys(handlers).length === 0)
          ? undefined
          : typeof handlers === "function"
          ? handlers
          : undefined; // FIXME: Method handler
        const errorComponents = components.slice();
        if (mod.component !== null) {