Last updated Jul 8, 2021

Rate this page:

Add routing to a custom UI full page app

This page describes how to add routing to a full page app created with Forge, using React and React Router. Routing enables your app to manipulate the current page URL. Routing may be used to enable users to link directly to certain parts of your app.

Before you begin

This guide assumes you're already familiar with developing on Forge. If this is your first time using Forge, see Getting started for step-by-step instructions on setting up Forge.

This guide also assumes you're familiar with developing custom UI apps on Forge. If this is your first time developing a custom UI app, see Build a custom UI app in Jira for detailed instructions on developing with custom UI.

Create a custom UI Jira admin page app

This guide will use the jira:adminPage module with custom UI, but you can add routing to any of the modules where the createHistory API is available.

  1. Navigate to the directory where you want to create the app. A new directory with the app’s name will be created there.
  2. Create your app by running:

    1
    forge create
    1. Enter a name for your app. For example, custom-ui-routing-tutorial.
    2. Select the Custom UI category.
    3. Select the jira-admin-page template.
  3. Change to the app subdirectory to see the app files.

    1
    cd custom-ui-routing-tutorial

Build the content for your custom UI

You need to install and build these resources so your app can use them. Follow these steps to build the resources for your app:

  1. Navigate to the static/hello-world directory.

  2. Install the needed dependencies:

    1
    npm install
  3. Build the assets:

    1
    npm run build
  4. Navigate back to the top-level directory of your app.

Deploy and install your app

Any time you make changes to your app code, rebuild the static frontend as prescribed above and then run a deploy using the forge deploy command. This command compiles your FaaS code, and deploys your functions and static assets to the Atlassian cloud.

To install your app on a new site, run the forge install command. Once the app is installed on a site, it will automatically pick up all minor app deployments, which means you don't need to run the install command again. A minor deployment includes any change that doesn't modify app permissions in the manifest.

  1. Navigate to the app's top-level directory and deploy your app by running:

    1
    forge deploy
  2. Install your app by running:

    1
    forge install
    1. Select Jira using the arrow keys and press the enter key.

    2. Enter the URL for your development site. For example, example.atlassian.net.

      View a list of your active sites at Atlassian administration.

Once the successful installation message appears, your app is installed and ready to use on the specified site.

Note, you need to run forge deploy before running forge install in any of the Forge environments.

Visit https://<YOUR_SITE>.atlassian.net/plugins/servlet/upm and find your app in the list of apps on the left side of the page.

Implement routing in your app

Follow these steps to use React Router in your custom UI app:

  1. Navigate to the static/hello-world directory.
  2. Install the React Router library:

    1
    npm install react-router
  3. Ensure you have the latest @forge/bridge version installed:

    1
    npm install @forge/bridge@latest
  4. Open the static/hello-world/src/App.js file. Use React Router to create an application with two screens on different routes by modifying the content of the file to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { Fragment, useEffect, useState, useContext } from "react";
import { view } from "@forge/bridge";
import { Router, Route, Switch, __RouterContext } from "react-router";

function Link({ to, children }) {
  const { history } = useContext(__RouterContext);
  return (
    <a
      href={to}
      onClick={(event) => {
        event.preventDefault();
        history.push(to);
      }}
    >
      {children}
    </a>
  );
}

function Home() {
  return (
    <Fragment>
      <h2>Home</h2>
      <Link to="/page-with-path">Route to page with path</Link>
    </Fragment>
  );
}

function PageWithPath() {
  return <h2>Page with path</h2>;
}

function App() {
  const [history, setHistory] = useState(null);

  useEffect(() => {
    view.createHistory().then((newHistory) => {
      setHistory(newHistory);
    });
  }, []);

  return (
    <div>
      {history ? (
        <Router history={history}>
          <Switch>
            <Route path="/page-with-path">
              <PageWithPath />
            </Route>
            <Route path="/">
              <Home />
            </Route>
          </Switch>
        </Router>
      ) : (
        "Loading..."
      )}
    </div>
  );
}

export default App;
  1. Rebuild the static assets for your custom UI frontend by running the npm run build command from the static/hello-world directory.

  2. Navigate to the app's top-level directory and start a tunnel for your app by running:

    1
    forge tunnel

    You can see your changes by refreshing the page that your app is on.

  3. Redeploy your app by running the forge deploy command.

You now have a full page app with routing:

Example app with routing

Rate this page: