Giter Club home page Giter Club logo

astro-pages's Introduction

astro-pages

npm version readme

Add custom file based routing directories in Astro

Examples

Astro Integration

// astro.config.mjs
import { defineConfig } from 'astro/config';
import pages from 'astro-pages';

export default defineConfig({
  // Inject pages inside 'src/routes'
  integrations: [ pages('routes') ],
});

Standalone Utility

// package/index.ts
import type { AstroIntegration } from 'astro';
import { addPageDir } from 'astro-pages';

export default function(options): AstroIntegration {  
  return {
    name: 'astro-pages',
    hooks: {
      'astro:config:setup': ({ config, logger, injectRoute }) => {

        const pageConfig = {
          cwd: import.meta.url,
          dir: 'pages',
          config,
          logger
        }

        const { 
          pages,
          injectPages 
        } = addPageDir(pageConfig)

        // Injects pages inside 'package/pages'
        injectPages(injectRoute)

      }
    }
  }
}

astro-pages's People

Contributors

brycerussell avatar github-actions[bot] avatar florian-lefebvre avatar dependabot[bot] avatar

Stargazers

Goth ight avatar Chloe Arciniega avatar  avatar Nikola Hristov avatar David Gidwani avatar $hibaKing avatar  avatar Tony O'Connell avatar Andrejs Agejevs avatar Zander Martineau avatar Kabolobari Benakole avatar  avatar

Watchers

 avatar Prodkt avatar

astro-pages's Issues

Fix issues with types

Currently, the types for astro-pages break in larger projects like astro-theme-provider and @astrolicious/i18n but seems works in brand new empty projects. For some reason, the types on the function signatures is any but importing the types and casting them manually works. Why is this happening?

Update `addPageDir` API for integrations

Currently the API for integrations looks like this:

const {
  patterns,
  entrypoints,
  injectPages()
} = addPageDir({...})

injectPages()

The problem with this API is that entrypoints is not a live array, if the patterns object is updated it will not be reflected inside entrypoints. Instead we should encourage the user to use Object.values(patterns) to compile an array of entrypoints from the live pattern reference.

Another tweak that could be made is renaming patterns to routes or pages. patterns is an object of pattern keys and entrypoint values so it would make since to rename this something more semantically correct

Also need to document manipulating the patterns object to add/remvoe pages

Future

Maybe adding utilities like addPage or removePage, although this can already be done fairly easily by manipulating the patterns object.

Check integration comes before other integratrations

Some Astro integrations will throw errors if astro-pages is located after the integration.

To do:

  1. Explore compatibility by downloading Astro integrations and testing them before/after astro-pages in the integrations array
  2. If an integration throws errors when using astro-pages:
    • Add a check using hasIntegration
    • Or, comment here for someone else to add

Reported incompatibilities:

  • astro-pages after @storyblok/astro

Support Astro v3.0

In Astro v4.0, the entryPoint property for the injectRoute option was renamed to entrypoint (lowercase p):
https://docs.astro.build/en/guides/upgrade-to/v4/#renamed-entrypoint-integrations-api

Currently astro-pages only uses the property entrypoint but could also add the property entryPoint to support v3.0

   injectRoute({
+   entryPoint: entrypoint,
    entrypoint,
    pattern
   })

Astro is easy to upgrade and users should always be using the latest version but in some cases a project might be "stuck" in an older version so there is no reason to not support this since it is a non-breaking change

Move `injectRoute` argument from `addPageDir` to `injectPages`

Currently, when using the standalone utility, you have to pass the injectRoute function as argument to addPageDir:

const pageConfig = {
  dir: 'pages',
  config,
  logger,
  injectRoute
}

const { 
  pages,
  injectPages 
} = addPageDir(pageConfig)

injectPages()

But in some cases a user might only want to access the pages object and NOT inject the pages so it would make more sense if injectRoute was an argument for injectPages instead of addPageDir:

const pageConfig = {
  dir: 'pages',
  config,
  logger
}

const { 
  pages,
  injectPages 
} = addPageDir(pageConfig)

injectPages(injectRoute)

Know if a page is prerendered

Would be great to update the format of pages to be an InjectedRoute, including prerender. I guess it requires reading each file and some regex work

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.