Giter Club home page Giter Club logo

vip-decoupled-bundle's Introduction

WordPress VIP Decoupled Plugin Bundle

This plugin bundle provides a number of plugins to help you quickly setup a decoupled WordPress application. It is designed to support VIP’s Next.js boilerplate but can be used to support any decoupled frontend. It solves a number of common problems facing decoupled sites, including:

  • Previewing
  • Permalinks
  • Feeds
  • Exposing structured data for block-based content

⚠️ This project is under active development. If you are a VIP customer, please let us know if you'd like to use this plugin and we can provide additional guidance. Issues and PRs are welcome. 💖

Table of contents

Installation

The latest version of the plugin can be downloaded from the repository's Releases page. Unzip the downloaded plugin and add it to the plugins/ directory of your site's GitHub repository.

Plugin activation

For VIP sites, we recommend activating plugins with code.

For Non-VIP sites, activate the plugin in the WordPress Admin dashboard using the following steps:

  1. Navigate to the WordPress Admin dashboard as a logged-in user.
  2. Select Plugins from the lefthand navigation menu.
  3. Locate the "VIP Decoupled Plugin Bundle" plugin in the list and select the "Activate" link located below it.

Getting started

This plugin is ready for local development using wp-env and Docker:

wp-env start

This command will start a local WordPress environment, activate the plugin, and be ready for GraphQL requests from our Next.js boilerplate (which must be set up separately).

The default credentials for the Admin Dashboard (provided by wp-env) are U: admin / P: password.

Configuration

Setting the home URL

WordPress needs to know the address of your frontend so that it can point previews, permalinks, and other URLs to the correct destination. WordPress uses the home option for this, but by default it is set to the same address that WordPress is served from. This plugin requires you to update home to the address of your decoupled frontend. Additionally, it handles a few edge cases, like feed URLs, that we want to serve from WordPress (see ./urls/urls.php).

If you are using wp-env for local development, this step is already done for you in .wp-env.json. By default, it points to http://localhost:3000; if you make changes, you'll need to stop and start your environment for the changes to take effect.

For other environments, you'll need to make this change manually. The easiest method is using WP-CLI:

wp option update home "https://my-decoupled-frontend.example.com"

You can also define a constant in code that overrides the value of the option:

define( 'WP_HOME', 'https://my-decoupled-frontend.example.com' );

It is also possible to make this change in the Admin Dashboard, but be careful: In the Admin Dashboard, WordPress labels the home option inconsistently and in ways that can be confusing and misleading. A related but separate option named siteurl governs where WordPress serves the Dashboard and other core functionality. You should not edit siteurl; however, WordPress sometimes labels the home option as "Site Address (URL)."

Multisite installations require different configuration. Please see [MULTISITE.md][multisite-file].

Jetpack Configuration

Prior to version 11.2, Jetpack had syncing functionality that fires on WordPress shutdown hooks. This can cause performance issues for API requests. A more performant architecture is used in all versions of Jetpack since 11.2. If you are unable to update Jetpack, you can install a VIP written plugin to offload the sync to cron. It also detects the verson of Jetpack and will not conflict if you upgrade Jetpack in the future.

Plugin settings

This plugin provides a settings page in the Admin Dashboard, found at Settings > VIP Decoupled. There, you'll find your GraphQL endpoint. You can also see (and optionally disable) the "sub-plugins", described below, that this plugin provides.

That's all the configuration that's needed to support your decoupled frontend. If you are using VIP's Next.js boilerplate, head over to the README to get your frontend up and running.

Sub-plugins

This plugin bundle provides a number of "sub-plugins" that are commonly useful in decoupled environments. You are free to disable any of them and bring your own alternatives.

WPGraphQL

WPGraphQL is a GraphQL API for WordPress, and provides the backbone of how your decoupled frontend will load content from WordPress. GraphQL is a relatively new but very powerful query language that provide a good developer experience.

When updates are pushed out to WPGraphQL, we will update this plugin after evaluating it for compatibility and performance. If you need to run a different version of WPGraphQL, you can disable the bundled version and activate your own.

WPGraphQL Preview

This plugin overrides WordPress's native preview functionality and securely sends you to your decoupled frontend to preview your content. This ensures that your preview content has parity with your published content. It works by issuing a one-time use token, locked to a specific post, that can be redeemed by the frontend to obtain preview content for that post.

This plugin currently only works with our Next.js boilerplate and should be disabled if you are not using it. If you are interested in using this plugin for other frontend frameworks, please see the preview README.

Block Data Plugins

There are 2 sub-plugins bundled for exposing the Gutenberg blocks - WPGraphQL Content Blocks and VIP Block Data API. In the near future, WPGraphQL Content Blocks will be deprecated in favour of just including the VIP Block Data API.

WPGraphQL Content blocks

This plugin exposes Gutenberg blocks as a field named contentBlocks on all post types that support a content editor:

query AllPosts {
  posts {
    nodes {
      id
      title
      contentBlocks {
        blocks {
          attributes {
            name
            value
          }
          name
          innerHTML
        }
        isGutenberg
        version
      }
    }
  }
}

This will allow you to easily map Gutenberg blocks to front-end components. Posts that do not support Gutenberg will return a single content block with the block name core/classic-editor, which contains the full innerHTML of the post.

See our Next.js boilerplate for an example of how to use and render these blocks.

VIP Block Data API

This plugin exposes Gutenberg blocks as JSON data, with integrations for both the official WordPress REST API and WPGraphQL. The GraphQL API exposes the blocks under a field name blocksData on all post types that support a content editor:

query GetPost {
  post(id: "1", idType: DATABASE_ID) {
    blocksData {
      blocks {
        id
        name
        attributes {
          name
          value
        }
        innerBlocks {
          name
          parentId
          id
          attributes {
            name
            value
          }
        }
      }
    }
  }
}

Posts that do not support Gutenberg are not supported by this plugin. For more information, refer to the documentation here.

Which Plugin Should I Choose?

We recommend the VIP Block Data API plugin, as the plugin of choice for exposing the Gutenberg blocks.

However, if you require exposing the Gutenberg blocks as HTML structured data rather than JSON data, then using the WPGraphQL Content Blocks plugin would be recommended. This would allow for the decoupled app, to easily render the blocks via the dangerouslySetInnerHTML method rather than having to individually design each block's corresponding component.

That being said, we intend to get this feature included in the VIP Block Data API plugin in the near future. This would allow for the eventual deprecation of the WPGraphQL Content Block plugin.

Contributing

Refer here for how to contribute to this plugin's development.

vip-decoupled-bundle's People

Contributors

alecgeatches avatar artskidesign avatar chriszarate avatar elazzabi avatar ingeniumed avatar mjangda avatar renatonascalves avatar sjinks avatar smithjw1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

vip-decoupled-bundle's Issues

Support local previews

Currently previewing is only supported on the home_url of the current site.
https://github.com/Automattic/vip-decoupled-bundle/blob/trunk/preview/preview.php#L35

When a site is being used with the VIP Development Environment, the Node.js site often doesn't live at that URL. It lives at one more like http://localhost:9000.

Could we add a filter to allow a customer to pass in a static base URL for previews? That would allow for local development and applications where the home_url set in WordPress is not the same as the URL of the Node.js application.

GraphQL registration can cause warnings

The WPCOMVIP\Decoupled\Registration\register_custom_post_types() function checks for two array keys, _builtin and public, which may not exist when this function is called. This function is a callback for the register_post_type_args hook, which is called before the WP_Post_Type class adds default args to the post type args passed to the constructor. If those array keys do not exists when GraphQL registration checks their value, a PHP warning will occur.

See: https://github.com/Automattic/vip-decoupled-bundle/blob/trunk/registration/registration.php#L19-L20.

A possible fix is to do an isset() check on those keys before checking their values.

Request to Add Filter for Customizing Maximum Token Lifetime

I have encountered a limitation regarding the maximum token lifetime, which is hard-coded to 3 hours ($max_lifetime = 3 * HOUR_IN_SECONDS;). This limitation is restrictive for my use case, where I require a longer token lifetime.

Proposed Feature:
I propose the addition of a filter in the get_token_lifetime_in_seconds function to allow users to modify the $max_lifetime value. This change would provide greater flexibility in managing token lifetimes according to different use cases.

Suggested Implementation:
The modification could be as simple as adding a filter hook in the function that determines the token lifetime. For example:

$max_lifetime = apply_filters( 'vip_decoupled_max_token_lifetime', 3 * HOUR_IN_SECONDS, $action, $post_id );

This change would allow developers to hook into this filter and set a custom maximum lifetime without directly modifying the plugin's core code.

Benefits of the Change:
Provides flexibility for different use cases that require longer or shorter token lifetimes.
Enhances the plugin's adaptability to various user requirements.
Maintains backward compatibility, as users who do not hook into this new filter will continue to use the default 3-hour limit.

Additional Context:
In my specific scenario, I need to extend the token lifetime to [describe your specific requirement, e.g., 1 year], which is currently not possible due to the hard-coded limit. This feature would significantly improve the plugin's utility in scenarios similar to mine.

Thank you for considering this feature request. I believe it would be a valuable addition to and would greatly appreciate any feedback or thoughts on this proposal.

Cross-site cookies

Hello, we're trying to get the project up and running with a Next.js app with login functionality.

In local development we've already configured the subsite "http://manage.3000.vipdev.lndo.site/" to point its home to "http://localhost:3000" where our Nextjs app is located, and this results in a "301 Moved Permanently" which forces navigation to localhost.

However after login via GraphQL mutation we get a "Set-Cookie" response header which is ignored because it references a different domain ".3000.vipdev.lndo.site".

So the question would be how do we redirect a subsite to the Nextjs app without losing the domain in the URL.
Thank you in advance.

Screenshot 2021-11-29 at 10 34 59

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.