npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

inertia-sails

v0.2.2

Published

The Sails adapter for Inertia.

Downloads

54

Readme

Inertia.js Sails Adapter

Installation

Backend

The quickest way to get setup an Inertia powered Sails app is to use the create-sails scaffolding tool. Just run

  npx create-sails <project-name>

Do replace <project-name> with the name you want your project to be.

Frontend

If you are using the create-sails scaffolding tool then the Frontend framework you choose from the CLI prompt should already be setup for you.

Usage

Responses

Sending back an Inertia responses is pretty simple, just use the sails.inertia.render method in your Sails actions(You can look at the example actions if you used create-sails). The render method accepts two arguments, the first is the name of the component you want to render from within your pages directory in assets/js (without the file extension).

The second argument is the props object where you can provide props to your components.

Shared Data

If you have data that you want to be provided as a prop to every component (a common use-case is informationa about the authenticated user) you can use the sails.inertia.share method.

In Sails having a custom hook by running sails generate hook custom will scaffolding a project level hook in which you can share the loggedIn user information for example. Below is a real world use case:

/**
 * custom hook
 *
 * @description :: A hook definition.  Extends Sails by adding shadow routes, implicit actions, and/or initialization logic.
 * @docs        :: https://sailsjs.com/docs/concepts/extending-sails/hooks
 */

module.exports = function defineCustomHook(sails) {
  return {
    /**
     * Runs when this Sails app loads/lifts.
     */
    initialize: async function () {
      sails.log.info('Initializing custom hook (`custom`)')
    },
    routes: {
      before: {
        'GET /': {
          skipAssets: true,
          fn: async function (req, res, next) {
            if (req.session.userId) {
              const loggedInUser = await User.findOne({
                id: req.session.userId
              })
              if (!loggedInUser) {
                sails.log.warn(
                  'Somehow, the user record for the logged-in user (`' +
                    req.session.userId +
                    '`) has gone missing....'
                )
                delete req.session.userId
                return res.redirect('/signin')
              }
              sails.inertia.share('loggedInUser', loggedInUser)
              return next()
            }
            return next()
          }
        }
      }
    }
  }
}

Configuration

If you used the create-sails scaffolding tool, you will find the configuration file for Inertia.js in config/inertia.js. You will mostly use this file for asset-versioning in Inertia by setting either a number or string that you can update when your assets changes. Below is an example of how this file looks like:

/**
 * Inertia configuration
 * (sails.config.inertia)
 *
 * Use the settings below to configure session integration in your app.
 *
 * For more information on Inertia configuration, visit:
 * https://inertia-sails.sailscasts.com
 */

module.exports.inertia = {
  /**
   * https://inertiajs.com/asset-versioning
   * You can pass a string, number that changes when your assets change
   *  or a function that returns the said string, number.
   * e.g 4 or () => 4
   */
  // version: 1,
}

Visit inertiajs.com to learn more.